You are on page 1of 75

Android activity launch modes and tasks

Gonalo Silva / Novoda

Co-organizer ~1250 members

Activity launch modes and tasks..

What is an Activity?

Main building block of Android applications
public class HelloWorldActivity extends Activity { .... }

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="" package="com.novoda.jax" android:versionCode="1" android:versionName="1.0" > <application > <activity android:name=".HelloWorldActivity" /> </application>


Activity task

Sequence of related activities a user follows

Activity stack / back stack

How are activites started?

Messaging facility for late run-time binding between components

Intent object

Data URI and/or MIME type

Category Extras key/value pairs Component name Flags

Explicit intent resolution

Intent intent = new Intent(this, HelloWorldActivity.class);


Implicit intent resolution

Uri google = Uri.parse(""); Intent intent = new Intent( Intent.ACTION_VIEW, google); startActivity(intent);

Intent filter
<activity android:name=".SimpleBrowser" > <intent-filter > <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="http" /> </intent-filter> </activity>

Intents are great!

Loosely coupled activities Specify your behaviour as intentions

Activity re-use

Starting an activity in a different application

Twitter Task

Twitter Task

Gmail Twitter Task

Activity re-use

Some activities are started in a different task

Gmail Task A


Task B
Gmail Task A

Activity re-use
Why different tasks?

Allows user to resume after leaving original task

How can we resume an activity?

Resuming an activity

Resuming a killed activity

How can we choose a specific task behaviour for our activities?

Activity launch modes

Define how a new instance of an activity is associated with the current task

Four launch modes

standard singleTop singleTask singleInstance

Defined in the manifest

<activity android:launchMode="singleTop" android:name=".HelloWorldActivity" />

Set as a flag in an intent

Intent intent = new Intent(this, HelloWorldActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(intent);

Demo App!!!
Search the market for: launchmode

standard default launch mode

A new activity X will not be started if there is an activity X on top of the stack onNewIntent() will be called instead of the normal activity lifecycle

Created at the root of a new task

Only one instance of the activity can exist at a time

onNewIntent() is called if an instance of the activity already exists


Like singleTop but the only member of its task Any activities started will open in a separate task.

Examples of launchModes
singleTop: Dashboard, Search
singleTask: Browser, Google Maps singleInstance: Home screen

Intent flags and launchModes


Force an activity to a new task


Not recommended!





The new activity is not kept in the list of recently launched activities


Other manifest attributes to help deal with activities and tasks

Useful to ensure certain activities are always in the same task By default, all activities in an application have the same affinity

singleTask Gotcha!!!


Next time an activity comes to the front, move it to a task it has an affinity for Eg: email and browser


Clear all activities from the task except the root, when launching the app from the home screen Only honoured for activities that start a new task

Can be combined with allowTaskReparenting


The state of the task is mantained by the system, even after a long period of time Only honoured for activities that start a new task

Eg: browser tabs


Shut down an instance of an activity when its task is resumed Trumps android:alwaysRetainTaskState

Use with caution!

singleTask and singleInstance change the UI flow

singleTask UI flow

singleInstance UI flow

Two launcher icons

Provide a launcher icon for each singleTask / singleInstance activity

Another disadvantage:

Recommended launchModes:

standard and singleTop with intent flags combinations will suit most use cases

To recap:
Activities and how they live in tasks

Intents and re-using activities in other apps

Activity lifecycle and the four launchmodes Intent flags and manifest attributes singleTask and singleInstance disadvantages

Thank you for listening! Any Questions?