This Presentation talks about various aspects of managing the Activity Backstack.
Please note this was prepared last year but most of the concepts are still relevant
Breaking the Kubernetes Kill Chain: Host Path Mount
Â
Managing Activity Backstack
1. Managing Android
Back stack
Rajdeep Dua
June 2012
Monday, March 18, 13
2. Agenda
⢠Activity Back Stack explained
⢠Tasks
⢠Bringing activities to the foreground
⢠Modifying Activity launch behavior using manifest
ďŹle attributes
⢠Other advanced behaviors
⢠No History
⢠Task Reset
⢠Task AfďŹnity
Monday, March 18, 13
3. Android Back Stack
⢠An Android application is composed of multiple
activities
⢠New Activities are launched using Intents
⢠Older ones are either destroyed or placed on a
back stack
Monday, March 18, 13
4. Android Back Stack
⢠New Activity can be launched in the same task or
a new task
⢠Back stack is a data structure which holds activities
in the background which user can navigate to using
back button.
⢠Back Stack works in the last in ďŹrst out mode.
⢠It can be cleared in case of low memory
Monday, March 18, 13
5. Tasks
⢠Task is a collection of activities that user performs while
doing a job
⢠An activity is launched in a new task from the Home
screen
⢠By default all the activities in an application belong to the
same task
⢠Activities in a task can exist in the foreground,
background as well as on the back stack.
Monday, March 18, 13
6. Tasks
Activity A and Activity B are launched in Task A.
By default all activities in the same application
belong to a single task.
Monday, March 18, 13
7. Bringing Activities
to the Foreground
⢠Activities pop back to
foreground as the user
presses back button
⢠As an example in the
ďŹgure
⢠Activity B gets
destroyed as soon as
user presses back
button
⢠Activity A comes to
the foreground
Monday, March 18, 13
8. Task in the Background
⢠Application 1 Launches in Task A
⢠Home Screen Task A Goes to the Background
⢠Application 2 Launches in Task B in the foreground
Monday, March 18, 13
10. Activity Launch Mode
⢠Activities can be conďŹgured to launch with the
following modes in the Manifest ďŹle
Use Case Launch Mode
standard
Normal
singleTop
singleTask
Specialized
singleInstance
Monday, March 18, 13
11. Activity Launch Mode
standard
<activity android: launchMode=âstandardâ/>
⢠Default Launch
Mode
⢠All the Activities
belong to the
same task in the
application
Monday, March 18, 13
12. Activity Launch Mode
singleTop
<activity
android:launchMode=âsingleTopâ/>
⢠Single instance of an
Activity can exist at
the top of the back
stack
⢠Multiple instances can
exist if there are
activities between
them
Monday, March 18, 13
13. Activity Launch Mode
singleTask
<activity android:launchMode=âsingleTaskâ/>
⢠Activity is the root of a
task
⢠Other activities are part
of this task if they are
launched from this activity
⢠If this activity already
exists Intent is routed to
that Instance
Monday, March 18, 13
14. Activity Launch Mode
singleTask...contd
⢠If this activity
already exists
Intent is routed to
that instance and
onNewIntent()
Lifecycle method
is called
Monday, March 18, 13
15. Activity Launch Mode
singleInstance
<activity
android:launchMode=âsingleInstanceâ/>
⢠Activity B is launched
with launchMode
singleInstance, it is
always the only
activity of its task
Monday, March 18, 13
16. SingleTop using Intent Flags
⢠Single Instance of an Activity at the top of the back
stack can also be achieved using Intent Flags.
Step 1 : Create Activity A using an Intent with no flag
Intent intentA = new Intent(MainActivity.this,
ActivityA.class);
startActivity(intentA);
Step 2 : Create Activity A again with the Intent flag FLAG_ACTIVITY_SINGLE_TOP
Intent intentA = new Intent(MainActivity.this,
ActivityA.class);
intentA.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
startActivity(intentA);
Monday, March 18, 13
17. Other Behaviors..
⢠No History on the Back stack
⢠Task Reset and clearing the
Back Stack
⢠Task AfďŹnity attribute in
Manifest
Monday, March 18, 13
18. No History on the Back Stack
⢠An Activity can be set to have no history on the back stack - it will be destroyed
as soon as user moves away from it using the the intent ďŹag
FLAG_ACTIVITY_NO_HISTORY
Monday, March 18, 13
19. Clear Activity on Task Reset
⢠Activities can be cleared from the back stack when a
task is reset.
⢠Use the Intent Flags FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
and FLAG_ACTIVITY_RESET_TASK_IF_NEEDED are used together
to clear activities on a task reset.
⢠Flag FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET  to indicate
that the activity will be destroyed when the task is reset.
⢠Flag FLAG_ACTIVITY_RESET_TASK_IF_NEEDED refers to the activity in
the back stack beyond which activities are destroyed.
Monday, March 18, 13
21. Clear Activity on Task Reset
Sample Code
//Code below shows how the Activity A gets launched with
the appropriate flags.
Intent intentA = new Intent(MainActivity.this,
ActivityA.class);
intentA.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED)
;
startActivity(intentA);
//sample code below shows the flag with with Activity B and
Activity C get launched.
Intent intentB = new Intent(ActivityA.this,
ActivityB.class);
intentB.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
);
startActivity(intentB);
Monday, March 18, 13
22. TaskAfďŹnity
⢠Task AfďŹnity can be used to launch activities in a new task, e.g
having two Launcher Activities which need to have their own task
⢠EntryActivityA and EntryActivityB will Launch in their own tasks
Monday, March 18, 13
23. TaskAfďŹnity
..contd
⢠EntryActivityA and EntryActivityB will Launch in
their own tasks
⢠Manifest ďŹle entries for the two activities
<activity android:name=".EntryActivityA"
android:label="@string/activitya"
android:taskAffinity="stacksample.activitya">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".EntryActivityB"
android:label="@string/activityb"
android:taskAffinity="stacksample.activityb">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Monday, March 18, 13
24. Summary
⢠Default behavior of using the back stack serves
most of the use cases
⢠Default behavior can be modiďŹed using Manifest
ďŹle attributes and Intent ďŹags to launch activities in
their own task, having a single instance or having
no history
Monday, March 18, 13