Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The following question has been presented to me while solving an exam on the Android and iOS systems, and despite understanding the Android backstack, i have no idea how to answer it.

How could you implement an Android application which relied on 5 Activites, and, upon openning them in the order 1 -> 2 -> 3 -> 4 -> 5, when in the 5th Activity the "back" button was pressed, the 5th Activity would close, allowing the 3rd to pop-up, and when "back" was pressed again, the same between the 3rd and 1st Activities. All this while also closing the 2nd and 4th Activities.
The question required a reply with emphasis on the Android system's backstack.

Backstack:
(all 5 activities are openned in the abovementioned order)
1, 2, 3, 4, 5

5th Activity closed
5, 4, 3, 2, 1
5, 3, 1

3rd Activity closed
3, 1
3, 1

1st Activity closed
1
Application closes

What I have tried:

I could very easily describe a programmatic implementation of what was requested, mainly tracking each Activity's openning order and upon closing the present one, if it was the 5th, i'd make the 2nd and 4th instantly close upon their onCreate() being called, allowing the odd-numbered Activities to appear to be the only existing ones.
However, this would not actually close the 2nd and 4th Activities and doesn't rely on the Backstack's functionality at all.
Posted
Updated 22-Jan-18 13:54pm
Comments
David Crow 22-Jan-18 23:03pm    
In Activity2:

startActivity(Activity3);
finish(); // this will remove Activity2 from the stack


And in Activity4:

startActivity(Activity5);
finish(); // this will remove Activity4 from the stack

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900