I would like to have this behavior in a task history stack:
- First task history stack is:
- Scenario 1.1: activity Main -> activity A -> activity B
- Next, activity B launches activity A, same instance
- Scenario 1.2: Main -> A -> B -> A (duplicate)
This way, if user presses back button, he goes back to B, and pressing back button again goes back to A (always same instance of A).
Using intent flag “FLAG_ACTIVITY_REORDER_TO_FRONT” I have achieved the behavior:
- First:
- Scenario 2.1: Main -> A -> B
- B launches A
- Scenario 2.2: Main -> B -> A
So same instance of A is actually brought to front, but after leaving B with back button, A is no longer between B and Main, so Main is shown.
- Is there any flag/activity-attribute or so, that can simplify achieving this behavior?
- Or do I need to handle “back button presses” on activity A?
- If so, assuming I am on scenario 2.2, how can I reorder A (after detecting back-button) to put it in between Main and B?
- Any other comments/suggestions will be appreciated.
Thank you!
I think that your desired scenario is normally impossible.Because of Tasks and Back Stack says:
So if your task be A -> B -> A (duplicate) and user presses back button, he goes back to B and A is destroyed and he can not go back to A.
I wrote tree activities A,A1,A2 that have this behavior:
Main -> A -> A1 -> A2 -> A1(duplicated)
Next when user presses BACK in A2:
A1 -> A2 -> A1(same) -> A -> Main
This is their codes:
Activity A is:
Activity A1 is:
Activity A2 is:
Note that I override onBackPressed() in activity A1 and you would to detect you want to back to A2 or A,So I Add an extra to intent and override onNewIntent(Intent intent) in A1.My project manifest is:
Pay attention to “singleInstance” and “singleTask” properties in Activities elements.Finally you can use this layouts for your activities to sure A1 is duplicated:
main.xml:
main1.xml:
main0.xml: