Reputation: 6612
I have a succession of multiple activities, and at some point I want to go in a different section of my app, and I don't want to have the previous activies in the stack. It's like this : Actvity A -> Activity A1 -> Activity A2 -> Activity A3 -> Activity B
When I start the activity B, I want to close all of the As activities.
The easiest way to do it I think is to setResult
then finish
the last activity and in the previous ones to look for the result and close the activity as well after passing the same data.
So I setResult
then finish
A3 before starting B.
The problem is that A3 is finished, but onActivityResult
isn't called on A2 until I close B so that A2 should be resumed.
I think onActivityResult
can't be propagated to multiple activities if another one is launched on top of it.
Then I'm blocked. I don't know what I should do or if there is another way to close multiples activities at once.
Upvotes: 2
Views: 6817
Reputation: 7486
You will have to start all A's activities with startActivityForResult(). When you start activity B,
setResult in A3 and finish A3
. As soon as A3 is finished, A2 onActivityResult()
will be called, setResult and finish it. As soon as A2 finishes, A1's onActivityResult will be called. Now do whatever you wanna do with the result of close the Activity.
Or you can start B from A after all child activities of A are finished.
Upvotes: 6
Reputation: 381
Assume you have started activity A1 and from A1 you started A2 now when you want to start A3 from A2 then,
finish();
setResult(0);
then A1's nativity will get called there check
if the resultCode==0 finish() then start A3
Upvotes: -2
Reputation: 17401
Only one activity can be active at a time so when you finish A3 and launch B which is now active to A2 cannot be called.
solution of this would be move to B in ActivityA
onActivityResult
.
Finish all the other activities and in first activity onActivityResult
launch B and finish.
if you are using API 11 and above you can just set a flag like:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
when you start ActivityB
. this will finish all the activities before ActivityB.
Upvotes: 2