Tasks and the Back Stack
In Sprout, the user opens the plant list, taps a Monstera, then taps Edit. Now they press Back twice. Where do they land each time? Android answers this with one simple data structure: a stack.
The rule
Start pushes, Back pops; a task is the stack.
A task is the collection of activities a user works with to do one job, kept as a back stack:
- Starting an activity pushes it on top. It becomes the one the user sees.
- Pressing Back pops the top activity. It is finished and destroyed, and the one underneath resumes.
- Calling
finish()in code pops the current activity, just like Back.
Each app launched from the home screen usually gets its own task, which is what you see as a card in the recents screen.
You can change the default "always push a new copy" behaviour when needed:
<activity android:name=".PlantListActivity"
android:launchMode="singleTop" />standard(default) — every start creates a new instance, even if one is already on the stack.singleTop— if the activity is already on top, reuse it (it receivesonNewIntent) instead of stacking a copy.singleTask— keep at most one instance in its task; starting it again clears the activities above it.
The intent flag FLAG_ACTIVITY_CLEAR_TOP does a similar clearing for one particular start.