109
Mastering Android Tasks

Manipulating Android tasks and back stack

Embed Size (px)

DESCRIPTION

This is the presentation I gave in Google TLV office on Nov 2011. It talks about how Android manages tasks and the tools we have to manipulate and control the default behavior

Citation preview

Page 1: Manipulating Android tasks and back stack

Mastering Android Tasks

Page 2: Manipulating Android tasks and back stack

Why I am on stage?

Ran is Android GDE.

Ran knows how to use Impress

Ran spent lot of time on Android tasks.

Ran can break-dance.

Page 3: Manipulating Android tasks and back stack
Page 4: Manipulating Android tasks and back stack

Activities

Single UI screen.

Stacked like a deck of cards.

Only one is visible.

Only one is active.

New Activities are places on top.

Page 5: Manipulating Android tasks and back stack

ActivitiesApplication may have more than one Activity

Page 6: Manipulating Android tasks and back stack

Task

Collection of activities.

Organized in stack (i.e. “back stack”)

Task have at least one activity.

New activities placed on top

LIFO Queue

Each task has a “name” called Affinity.

Activity A

Activity B

Activity C

Page 7: Manipulating Android tasks and back stack

Tasks

Each app has at least one taskMay have more

Tasks can be moved to “background”Keeping their back stack.

User navigate between tasksEasy switching between apps.

Multitasking experience.

Page 8: Manipulating Android tasks and back stack

(Backgorund Tasks)User's Action

Home

(Foreground Task)

Page 9: Manipulating Android tasks and back stack

(Backgorund Tasks)(Foreground Task)User's Action

Home

Ingress

Page 10: Manipulating Android tasks and back stack

(Backgorund Tasks)(Foreground Task)User's Action

Home

Ingress

Home

Page 11: Manipulating Android tasks and back stack

(Backgorund Tasks)(Foreground Task)User's Action

Home

Ingress

Home

G+

Page 12: Manipulating Android tasks and back stack

(Backgorund Tasks)(Foreground Task)User's Action

Home

Ingress

Home

G+

Enters Post

Page 13: Manipulating Android tasks and back stack

(Backgorund Tasks)(Foreground Task)User's Action

Home

Ingress

Home

G+

Enters Post

Home

Page 14: Manipulating Android tasks and back stack

(Backgorund Tasks)(Foreground Task)User's Action

Home

Ingress

Home

G+

Enters Post

Home

Flipboard

Page 15: Manipulating Android tasks and back stack

(Backgorund Tasks)(Foreground Task)User's Action

Home

Ingress

Home

G+

Enters Post

Home

Flipboard

Home

Page 16: Manipulating Android tasks and back stack

(Backgorund Tasks)(Foreground Task)User's Action

Home

Ingress

Home

G+

Enters Post

Home

Flipboard

Home

Recent Apps

Page 17: Manipulating Android tasks and back stack

(Backgorund Tasks)(Foreground Task)User's Action

Home

Ingress

Home

G+

Enters Post

Home

Flipboard

Home

Recent Apps

G+

Page 18: Manipulating Android tasks and back stack

Why Manipulate?

Page 19: Manipulating Android tasks and back stack

Why Manipulate default behavior?

Multiple entry points to the appLauncher

Notification

Share Intent

“Singleton” ActivityActivity that wants to share data between instances. (example: browser)

Page 20: Manipulating Android tasks and back stack

Changing default behavior

<activity> tag attributes in AndroidManifest.xmlLaunchmode

TaskAffinity

...

Adding flags to Intent used to launch the activityFLAG_ACTIVITY_NEW_TASK

FLAG_ACTIVITY_CLEAR_TOP

FLAG_ACTIVITY_SINGLE_TOP

Warning – only sounds easy.

Page 21: Manipulating Android tasks and back stack

Android:taskAffinity

Used to determine the task that should hold the activity

Task that has the same affinity value

By Default: all activities share the same affinity (package name)

Task's affinity is the affinity of the activity that created it (also called root activity)

Page 22: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Page 23: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Com.myApp

Activity A

Page 24: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A's code:

Intent I = new Intent (this,ActvitiyB.class)

startActvitiy(i)

Com.myApp

Activity A

Page 25: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A's code:

Intent I = new Intent (this,ActvitiyB.class)

startActvitiy(i)

Com.myApp

Activity A

Com.myApp

Activity A

Activity B

Page 26: Manipulating Android tasks and back stack

What?

Page 27: Manipulating Android tasks and back stack

Why like this?

Activity B added to the same task although it had different affinity

In order to create a new task – you need to add FLAG_ACTIVITY_NEW_TASK to the launching intent

Page 28: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Com.myApp

Activity A

Page 29: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A's code:

Intent I = new Intent (this,ActvitiyB.class)

I.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

startActvitiy(i)

Com.myApp

Activity A

Page 30: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A's code:

Intent I = new Intent (this,ActvitiyB.class)

I.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

startActvitiy(i)

Com.myApp

Activity B

Com.something

Activity A

Page 31: Manipulating Android tasks and back stack

Android:launchMode

Specifies how the Activity should be launched

Four different modes:Standard (default)

Single Top

Single Task

Single Instance

Page 32: Manipulating Android tasks and back stack

Launch Mode: Standard

New instance is created in the calling activity's task

Activity can have multiple instances

Instances can reside in different tasks

One task can have multiple instances

Page 33: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Com.myApp

Activity A

Page 34: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B (no flags)

Com.myApp

Activity A

Page 35: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B (no flags)

Com.myApp

Activity A

Activity B

Page 36: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B (no flags)

Activity B launches Activity A

Com.myApp

Activity A

Activity B

Page 37: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B (no flags)

Activity B launches Activity A

Com.myApp

Activity A

Activity B

Activity A

Page 38: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B (no flags)

Activity B launches Activity A

Activity A launches Activity B (with flag NEW_TASK)

Com.myApp

Activity A

Activity B

Activity A

Page 39: Manipulating Android tasks and back stack

ExampleManifest:

Com.myApp

Activity A

Activity B

Activity A

Activity B

Com.something

Activity A is launched

Activity A launches Activity B (no flags)

Activity B launches Activity A

Activity A launches Activity B (with flag NEW_TASK)

Page 40: Manipulating Android tasks and back stack

ExampleManifest:

Com.myApp

Activity A

Activity B

Activity A

Activity B

Com.something

Activity A is launched

Activity A launches Activity B (no flags)

Activity B launches Activity A

Activity A launches Activity B (with flag NEW_TASK)

Activity B launches Activity A

Page 41: Manipulating Android tasks and back stack

ExampleManifest:

Com.myApp

Activity A

Activity B

Activity A

Activity B

Com.something

Activity A is launched

Activity A launches Activity B (no flags)

Activity B launches Activity A

Activity A launches Activity B (with flag NEW_TASK)

Activity B launches Activity A

Activity A

Page 42: Manipulating Android tasks and back stack

Launch Mode: Single Top

If an instance of the activity is at the top of the stack – new activity will not be create, instead – onNewIntent() will be called.

Activity can have multiple instances

Instances can reside in different tasks

One task can have multiple instances

Page 43: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Com.myApp

Activity A

Page 44: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

User pulls the notification bar and clicks on notification that fires Activity intent to Activity A

Com.myApp

Activity A

Page 45: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

User pulls the notification bar and clicks on notification that fires Activity intent to Activity A

Com.myApp

Activity AOnNewIntent(..)

Page 46: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

User pulls the notification bar and clicks on notification that fires Activity intent to Activity A

Activity A launches Activity B (no flags)

Com.myApp

Activity AOnNewIntent(..)

Page 47: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

User pulls the notification bar and clicks on notification that fires Activity intent to Activity A

Activity A launches Activity B (no flags)

Com.myApp

Activity A

Activity B

Page 48: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

User pulls the notification bar and clicks on notification that fires Activity intent to Activity A

Activity A launches Activity B (no flags)

Activity B launches Activity A

Com.myApp

Activity A

Activity B

Page 49: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

User pulls the notification bar and clicks on notification that fires Activity intent to Activity A

Activity A launches Activity B (no flags)

Activity B launches Activity A

Com.myApp

Activity A

Activity B

Activity A

Page 50: Manipulating Android tasks and back stack

Launch Mode: Single Task

New task is created for the Activity and the Activity is its root Activity

If an instance of the Activity already exists – system will reroute the intent to onNewIntent() callback and won't create new instance.

Activity can have only one instance

Activity is always the root of the task

Page 51: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Com.myApp

Activity A

Page 52: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Acitivity A launches Activity B

Com.myApp

Activity A

Page 53: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Acitivity A launches Activity B

Com.myApp

Activity A

Activity B

Page 54: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Acitivity A launches Activity B

Activity B launches Activity C with FLAG_NEW_TASK

Com.myApp

Activity A

Activity B

Page 55: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Acitivity A launches Activity B

Activity B launches Activity C with FLAG_NEW_TASK

Com.myApp

Activity A

Activity B

Activity C

Com.something

Page 56: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Acitivity A launches Activity B

Activity B launches Activity C with FLAG_NEW_TASK

Activity C launches Activity A

Com.myApp

Activity A

Activity B

Activity C

Com.something

Page 57: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Acitivity A launches Activity B

Activity B launches Activity C with FLAG_NEW_TASK

Activity C launches Activity A

Com.myApp

Activity A Activity C

Com.something

OnNewIntent(..)

Page 58: Manipulating Android tasks and back stack

Launch Mode: Single Instance

Same as Single Task except that it is the only activity in its task

New activities will be launched in different tasks.

Activity can have only one instance

Activity is always the root of the task

Activity is the only member of its task

Page 59: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Com.myApp

Activity A

Page 60: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Com.myApp

Activity A

Page 61: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Com.myApp

Activity A

Com.myApp

Activity B

Page 62: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Activity B launches Activity C

Com.myApp

Activity A

Com.myApp

Activity B

Page 63: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Activity B launches Activity C

Com.myApp

Activity A

Com.myApp

Activity B

Activity C

Page 64: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Activity B launches Activity C

Activity C launches Activity A

Com.myApp

Activity A

Com.myApp

Activity B

Activity C

Page 65: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Activity B launches Activity C

Activity C launches Activity A

Com.myApp

Activity A

Com.myApp

Activity B

Activity C

OnNewIntent(..)

Page 66: Manipulating Android tasks and back stack

FLAG_ACTIVITY_NEW_TASK

Start the activity in a new task. If a task is already running for the activity you are now starting, that task is brought to the foreground with its last state restored and the activity receives the new intent in onNewIntent().

Page 67: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Com.myApp

Activity A

Page 68: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Com.myApp

Activity A

Page 69: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Com.myApp

Activity A

Activity B

Page 70: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Activity B launches Activity C with FLAG_NEW_TASK

Com.myApp

Activity A

Activity B

Page 71: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Activity B launches Activity C with FLAG_NEW_TASK

Com.myApp

Activity A

Activity B

Activity C

Com.something

Page 72: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Activity B launches Activity C with FLAG_NEW_TASK

Activity C launches Activity D

Com.myApp

Activity A

Activity B

Activity C

Com.something

Page 73: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Activity B launches Activity C with FLAG_NEW_TASK

Activity C launches Activity D

Com.myApp

Activity A

Activity B

Activity C

Com.something

Activity D

Page 74: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Activity B launches Activity C with FLAG_NEW_TASK

Activity C launches Activity D

User switches to com.myApp

Com.myApp

Activity A

Activity B

Activity C

Com.something

Activity D

Page 75: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Activity B launches Activity C with FLAG_NEW_TASK

Activity C launches Activity D

User switches to com.myApp

Activity B launches Activity C with FLAG_NEW_TASK

Com.myApp

Activity A

Activity B

Activity C

Com.something

Activity D

Page 76: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Activity B launches Activity C with FLAG_NEW_TASK

Activity C launches Activity D

User switches to com.myApp

Activity B launches Activity C with FLAG_NEW_TASK

Com.myApp

Activity A

Activity B

Activity C

Com.something

Activity D

onResume()

Page 77: Manipulating Android tasks and back stack

Easy – but may be tricky

Page 78: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Com.myApp

Activity A

Page 79: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Com.myApp

Activity A

Page 80: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Com.myApp

Activity A

Activity B

Page 81: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Activity B launches Activity C with FLAG_NEW_TASK

Com.myApp

Activity A

Activity B

Page 82: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Activity B launches Activity C with FLAG_NEW_TASK

Com.myApp

Activity A

Activity B

Activity C

Com.something

Page 83: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Activity B launches Activity C with FLAG_NEW_TASK

Activity C launches Activity D

Com.myApp

Activity A

Activity B

Activity C

Com.something

Page 84: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Activity B launches Activity C with FLAG_NEW_TASK

Activity C launches Activity D

Com.myApp

Activity A

Activity B

Activity C

Com.something

Activity D

Page 85: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Activity B launches Activity C with FLAG_NEW_TASK

Activity C launches Activity D

User switches to com.myApp

Com.myApp

Activity A

Activity B

Activity C

Com.something

Activity D

Page 86: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Activity B launches Activity C with FLAG_NEW_TASK

Activity C launches Activity D

User switches to com.myApp

Activity B launches Activity D with FLAG_NEW_TASK

Com.myApp

Activity A

Activity B

Activity C

Com.something

Activity D

Page 87: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Activity B launches Activity C with FLAG_NEW_TASK

Activity C launches Activity D

User switches to com.myApp

Activity B launches Activity D with FLAG_NEW_TASK

Com.myApp

Activity A

Activity B

Activity C

Com.something

Activity D

Activity D

Page 88: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Activity B launches Activity C with FLAG_NEW_TASK

Activity C launches Activity D

User switches to com.myApp

Activity B launches Activity D with FLAG_NEW_TASK

Com.myApp

Activity A

Activity B

Activity C

Com.something

Activity D

Activity D

Page 89: Manipulating Android tasks and back stack

New instance of D created!

Page 90: Manipulating Android tasks and back stack

Why another Instance was created?

A matching task is a task that has the same launching intent as the intent used to launch the activity.

Although Activity D has the same affinity as the task that was created by Activity C, the launching intent is different.

The result: instead of onResume() - > new Instance.

Page 91: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Com.myApp

Activity A

Page 92: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Com.myApp

Activity A

Page 93: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Com.myApp

Activity A

Activity B

Page 94: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Activity B launches Activity C with FLAG_NEW_TASK

Com.myApp

Activity A

Activity B

Page 95: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Activity B launches Activity C with FLAG_NEW_TASK

Com.myApp

Activity A

Activity B

Activity C

Com.something

Page 96: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Activity B launches Activity C with FLAG_NEW_TASK

Activity C launches Activity D and calls finish()

Com.myApp

Activity A

Activity B

Activity C

Com.something

Page 97: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Activity B launches Activity C with FLAG_NEW_TASK

Activity C launches Activity D and calls finish()

Com.myApp

Activity A

Activity B

Activity C

Com.something

Activity D

Page 98: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Activity B launches Activity C with FLAG_NEW_TASK

Activity C launches Activity D and calls finish()

User switches to com.myApp

Com.myApp

Activity A

Activity B

Activity C

Com.something

Activity D

Page 99: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Activity B launches Activity C with FLAG_NEW_TASK

Activity C launches Activity D and calls finish()

User switches to com.myApp

Activity B launches Activity C with FLAG_NEW_TASK

Com.myApp

Activity A

Activity B

Activity C

Com.something

Activity D

Page 100: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B

Activity B launches Activity C with FLAG_NEW_TASK

Activity C launches Activity D and calls finish()

User switches to com.myApp

Activity B launches Activity C with FLAG_NEW_TASK

Com.myApp

Activity A

Activity B

Activity C

Com.something

Activity D

onResume()

Page 101: Manipulating Android tasks and back stack

Asked for Activity C - > Got activiy D.

Page 102: Manipulating Android tasks and back stack

It is not over yet...

Page 103: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Com.myApp

Activity A

Page 104: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B with startActivityForResult()

Com.myApp

Activity A

Page 105: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B with startActivityForResult()

Com.myApp

Activity AOnActivityResult(...)

Page 106: Manipulating Android tasks and back stack

ExampleManifest:

Activity A is launched

Activity A launches Activity B with startActivityForResult()

Com.myApp

Activity A

Com.myApp

Activity B

Page 107: Manipulating Android tasks and back stack

StartActivityForResult

If an activity calls startActivityForResult and new task is created – onActivityResult is called immediately.

“its not bug – its a feature” approach.

Page 108: Manipulating Android tasks and back stack

Tips

Abd shell dumpsys activity

Don't wait for the end of the development to apply flags, affinities and launch modes.

It is iterative on-going process.