Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to finish every activity on the stack except the first in Android

Tags:

I'm porting an iPhone app to Android and I can't seem to find a means to pop each activity on the stack except the root activity.

In objective-c I would do something like the below

[navController popToRootViewControllerAnimated:YES]; 

Anyone know if I can effectively call "finish()" on each activity after some action?

like image 979
Toran Billups Avatar asked Jun 09 '11 19:06

Toran Billups


1 Answers

If you want to start one Activity, say, your homescreen, and remove every other Activity in your application's stack, you can use:

Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Removes other Activities from stack startActivity(intent); 

If you also want to provide this event in the MainActivity (such as a logo click in the title bar), you can add the FLAG_ACTIVITY_SINGLE_TOP flag as well to make sure it does not add another instance of itself to the stack.

like image 191
Glendon Trullinger Avatar answered Oct 11 '22 11:10

Glendon Trullinger