Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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.

The shortcut

Draw the stack as a column of names, newest on top. Every action is a push or a pop.

List, detail, edit, Back, BackStep 1 of 4
  1. Open Sprout

    Stack: List.

The trap: after a form saves, forgetting to call finish(). The user presses Back and the form reappears, looking unsaved.

Drills

Answer fast — trust the rule.

quick drill

The stack is List, Detail. The user taps Edit. What is the stack now?

quick drill

What happens to the top activity when the user presses Back?

quick drill

With the default launch mode, Detail is on top and it starts Detail again. What is on the stack?

quick drill

Which launch mode reuses the activity only when it is already at the top of the stack?

quick drill

After saving, Sprout's edit screen should not come back on Back. What should it call?

Remember

  • Start pushes, Back pops; a task is the stack.
  • Draw the column of names and apply each push or pop.
  • Call finish() when a screen's job is done; use launch modes only when the default stacking is wrong.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainTrace Sprout's back stack

In Sprout the user opens the plant list, taps a plant (detail activity), then taps Edit (edit activity) and saves. Trace the back stack after each step, and explain what should happen when saving if you do not want Back to return to the edit screen.