Issue
Imagine this activity history stack:
A > B > C > D > E
scenario 1:
If the user is on E then on tapping the back button it should navigate to D > C > B > A.
scenario 2:
If the user is on E then on tapping a custom button "Show B", then it should clear E > D > C. Which is similar to Finish().
Like X > Y if we set finish on Y the X will be displayed. Similar If I tape Show B on E then E > D > C should be cleared from the stack.
I need to achieve both scenarios.
(Edited ^^^^ with scenarios)
If the user is on E activity and wants to move B. If B is in history stack can we clear C > D > E so that user can navigate to B without startActivity(B). and A should be in history.
If an activity is available in the stack then it should load from history if not startActivity(B).
If I use FLAG_ACTIVITY_CLEAR_TOP/FLAG_ACTIVITY_NEW_TASK, it will clear full history and start's new activity.
I want to clear partial history.
Will it be possible to achieve? If so, how to do it please?
Solution
This is all pretty standard. Don't use any special launch modes. Normally, pressing BACK will just finish the current Activity and drop you back into the previous one.
For this case:
If the user is on E then on tapping a custom button "Show B", then it should clear E > D > C. Which is similar to Finish().
In E, to go back to the existing instance of B, do this:
Intent intent = new Intent(this, B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
This will finish E, D and C and return to the existing instance of B.
The flag FLAG_ACTIVITY_CLEAR_TOP tells Android to clear all activities between the current Activity and the target Activity. If you don't specify FLAG_ACTIVITY_SINGLE_TOP then the existing instance of the target Activity will also be finished and a new instance will be created. If you do specify FLAG_ACTIVITY_SINGLE_TOP then the existing instance of the target Activity will NOT be finished and a new instance will NOT be created.
Answered By - David Wasser
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.