Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear all activities in a task?

Tags:

android

I have a splash screen activity, then a login activity. My history stack looks like:

SplashActivity LoginActivity 

when the user successfully logs in via LoginActivity, I want to start WelcomeActivity, but clear the entire stack:

SplashActivity LoginActivity // launches WelcomeActivity -> WelcomeActivity  // but now all three are in the history stack, while I only // want WelcomeActivity in the stack at this point. 

Is there some flag I can use to do that?

// LoginActivity.java Intent intent = new Intent(this, WelcomeActivity.class); intent.addFlag(Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intent); finish(); 

Not sure if using the FLAG_ACTIVITY_CLEAR_TASK will clear out all activities in my task or not. I can do this 'manually' by unwinding the stack by using startActivityForResult() calls, but will be more fragile and more code to maintain.

Thanks

like image 537
user291701 Avatar asked May 12 '11 14:05

user291701


People also ask

How do you finish an activity?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.

How do I check my activity task?

Just open the perspective Windows->Open Perspective-> Hierarchy View In the list you can see the all the connected devices and emulators and the activity stack.


1 Answers

Yes that should work fine. You could use:

  • FLAG_ACTIVITY_CLEAR_TOP
  • FLAG_ACTIVITY_SINGLE_TOP
  • FLAG_ACTIVITY_CLEAR_TASK
  • FLAG_ACTIVITY_NEW_TASK

which ensures that if an instance is already running and is not top then anything on top of it will be cleared and it will be used, instead of starting a new instance (this useful once you've gone Welcome activity -> Activity A and then you want to get back to Welcome from A, but the extra flags shouldn't affect your case above).

like image 182
Joseph Earl Avatar answered Sep 29 '22 16:09

Joseph Earl