75
Android activity launch modes and tasks Gonçalo Silva / Novoda

Android | Android Activity Launch Modes and Tasks | Gonçalo Silva

Embed Size (px)

Citation preview

Android activity launch modes and tasks

Gonçalo Silva / Novoda

Co-organizer~1250 members

Activity launch modes and tasks..

What is an Activity?

Main building block of Android applications

HelloWorldActivity.java

public class HelloWorldActivity extends Activity {

....

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.novoda.jax" android:versionCode="1" android:versionName="1.0" >

<application >

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

</application>

</manifest>

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

Intents!

Intent object

• Action• Data – URI and/or MIME type• Category• Extras – key/value pairs• Component name• Flags

Explicit intent resolution

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

startActivity(intent);

Implicit intent resolution

Uri google = Uri.parse("http://www.google.com");

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

Task

Twitter

Task

Twitter

Task

Twitter

Gmail

Activity re-use

Some activities are started in a different task

Task A

Gmail

Task A

Gmail

Task B

Browser

Activity re-use

Allows user to resume after leaving original task

Why different tasks?

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

singleTop

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

singleTask

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

singleInstance

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

FLAG_ACTIVITY_SINGLE_TOP

FLAG_ACTIVITY_MULTIPLE_TASK

FLAG_ACTIVITY_NEW_TASK

Force an activity to a new task

Not recommended!

FLAG_ACTIVITY_NO_HISTORY

FLAG_ACTIVITY_CLEAR_TOP

FLAG_ACTIVITY_REORDER_TO_FRONT

FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS

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

FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESETFLAG_ACTIVITY_RESET_TASK_IF_NEEDED

Other manifest attributes to help deal with activities and tasks

android:taskAffinity

• Useful to ensure certain activities are always in the same task

• By default, all activities in an application have the same affinity

• singleTask Gotcha!!!

android:allowTaskReparenting

• Next time an activity comes to the front, move it to a task it has an affinity for

• Eg: email and browser

android:clearTaskOnLaunch

• 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”

android:alwaysRetainTaskState

• 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

android:finishOnTaskLaunch

• Shut down an instance of an activity when its task is resumed

• Trumps android:alwaysRetainTaskState

singleTask and singleInstance change the UI flow

Use with caution!

singleTask UI flow

singleInstance UI flow

Two launcher icons

Provide a launcher icon for each singleTask / singleInstance activity

Another disadvantage:

startActivityForResult()

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?