• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps finish activity on stack

satb

Lurker
I have a very simple use case. On the first activity (lets call it activity A), I have two buttons. One button for "sign UP" the user. The other button is for "sign IN". If the user taps on the "sign IN" button, then the username/password is form is presented. If the user successfully enters the username/password, then I don't want activity A anymore on the stack. On the other hand, if the user enters the wrong credentials on the "sign IN" screen, then I still want the user to be able to hit the back button and go back so I give the user an opportunity to "sign UP".

So the question is, from the "sign IN" screen, how can I finish activity A if the credentials entered are valid?

Put simply, root---(starts)--->A---(starts)---->B. Based on some event that occurs in activity B, I either need to keep or finish activity A. Is this possible?
 
Not sure about your specific use case, but I think it'd be doable with either:
Activity.finish();
or
Activity.moveTaskToBack(boolean nonRoot);

Activity | Android Developers


Thanks Kevin. For me to do an Activity.finish(), I have to have a reference to that activity. In the example I have -- where activity A starts activity B -- when I am in activity B, how can I get a reference to activity A so I can finish it? Please note that its only when an event happens in B (successful login), that I decide that I don't want A anymore on the stack. Otherwise, A still has to be on the stack on an unsuccessful login
 
Ah, I understand your problem now but I don't have a good answer.

A possibility:
In ActivityA initialize a SharedPreference, say "registered", to false.
ActivityB sets this to true when appropriate.
ActivityA's onResume() checks and calls finish() on itself if "registered" is true.

It's possible that will cause a double activity change animation or ActivityA to flash onto the screen or something though. And might delay getting back to Activity0.
 
Back
Top Bottom