Skip to the workspace
Pebellslearn by building
Activities and Their Lifecycle  ›  Capstone Project

Tasks and the Back Stack

Tasks & Backstack 15 minA task is a stack of activities the user moves through; starting an activity pushes it onto the stack and Back pops the top one off.

android / Tasks and the Back Stack

Explore each scene at your own pace.

01 · The rule

The rule

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 receives onNewIntent) 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.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.