Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to finish specific activities not all activities

In my android application suppose several activities are there if using intent I go to other activities like this

[Activity A]->[activity B]->[Activity C]->[Activity-D]->[Activity N]

and now when am on activity N when I pressed button then I want to go to Activity B and want to destroy Activity C And Activity D but Activity A should not destroy. I also searched in various posts but I didn't get exactly the same solution. Any help will be appriciated

like image 848
user121190 Avatar asked May 16 '16 06:05

user121190


1 Answers

In ActivityN, to return to ActivityB use this:

Intent intent = new Intent(this, ActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);

Using the flag CLEAR_TOP will finish all activities in the stack that are on top of ActivityB. In the example you gave, this will finish ActivityN, ActivityD and ActivityC. Using the flag SINGLE_TOP will make sure that this will return control to the existing instance of ActivityB (ie: it won't create a new instance of ActivityB).

like image 192
David Wasser Avatar answered Oct 14 '22 00:10

David Wasser