User7723337
User7723337

Reputation: 12018

Activity with Flag FLAG_ACTIVITY_CLEAR_TOP (android)

I have stack of activities launched after one another.
but in one of the activity I need to launch that particular activity with flag FLAG_ACTIVITY_CLEAR_TOP.

So that it will finish all the previous activities and start.

Problem I am facing that i don't want to finish one of the activity from the stack it should be presence on back of the newly launched activity.

Suppose I have activities [A] [B] [C] [D]
I am starting [D] activity with flag FLAG_ACTIVITY_CLEAR_TOP after starting activity [D] it will destroy all the activities, I want activity [B] to be kept running on the back and when we press back key on [D] it should display activity [B].

How to do this?

Thanks,
PP.

Upvotes: 5

Views: 9182

Answers (2)

Mudassir
Mudassir

Reputation: 13184

One solution is to call Activity [B] with flag FLAG_ACTIVITY_CLEAR_TOP, this will destroy all activities but [B]. After that, call Activity [D].

Upvotes: 5

While-E
While-E

Reputation: 1555

Like @Mudassir said, start activity [A]. When you call [B] simply use the FLAG to clear all data but [B], and then you can call [D] without flag from [B];thus having [B][D] like you wanted? Then on back button press you will go from [D] to [B], without having any other data?. You seem to not be thinking he wants you to call [B] with flag from [D], and what would the purpose of that be? You'd wind up with just [B] at that point.

-Either way, you lose [C] along the way. A solution might be:

startActivity(A)
[in activity A] startActivity(B) - WITH CLEAR_TOP TO KILL [A]
[in activity B] startActivityForResult(C) - WHEN [C] IS finish() IT WILL COME BACK TO [B]
[in activity B, onActivityResult()] startActivity(D)

This will give you JUST [B]&[D] while still getting to [C]....try it, it will work.

Upvotes: 3

Related Questions