1

I'm building an app that requires a user to have logged in and obtained a user id. This id is a member variable of my extended Application class.

I'd like the application to recognize when/if this variable gets wiped out and, if so, throw up my log in screen and destroy the Activity stack behind it.

Here's what I think I'm going to do:

Extend Activity and ListActivity (currently the only two types of Activities I am using). In the onResume() handler, check to see if the user id is set. If not, throw up the log in screen and destroy the rest of the Activity stack behind it. All of my Activities will derive from these new extended Activities.

My issue here is that I don't know how to destroy the Activity stack behind a particular Activity. Any ideas?

One idea is to have a broadcast receive listening for a particular message that tells Activities to kill themselves, but I'm hoping Android already has something in place for a situation like this.

Update:

Also, is there a way to clear the entire Activity stack? I'd like to override the onBackPressed() handler on an Activity and have the Activity stack blown out so the user gets taken back to the Android home screen.

Andrew
  • 20,756
  • 32
  • 99
  • 177
  • 2
    Use the FLAG_ACTIVITY_CLEAR_TOP (http://stackoverflow.com/questions/3973092/android-nuke-the-previous-app-history) – danh32 Feb 03 '11 at 16:05
  • Hmm... I am calling finish() on the log in Activity after the user logs in; so it is no longer on the stack, but I suppose I can not do this – Andrew Feb 03 '11 at 16:20
  • This works, though I would prefer to not have the login screen on the stack after the user logs in. I can use this if I need to, but do you have any ideas for placing a new Activity on the stack and removing everything under it? – Andrew Feb 03 '11 at 16:30

2 Answers2

7

Other choice will be using the noHistory parameter in the AndroidManifest.xml to accomplish this. so your Activity should not be placed in the history stack

<activity android:name=".MyActivity"
          android:noHistory="true" />
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
3

I agree with danh32, setting the FLAG_ACTIVITY_CLEAR_TOP on the intent to bring the Login Activity on top of the stack will destroy all activities behind the new activity (hence, the Login Activity). This will leave one activity on the stack.

Intent myIntent = new Intent(view.getContext(), DisplayMenu.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
finish();

I use this option to allow users to get back to a main screen from a long path through a lot of activities.

ice911
  • 1,180
  • 7
  • 9
  • 1
    why doesn't this work for me? I have LoginAct -> Map Act -> setting Act. In setting Act I click log out and run MyApplication.LogOutUser(); Intent loggedOut = new Intent(getApplicationContext(), LoginActivity.class);loggedOut.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(loggedOut); finish(); //finsih setting activity but now on LoginScreen when user clicks back I am back at MapAct! Why? Should minimize app since now Login should be only activity on stack... – topwik Nov 30 '12 at 17:48