Manipulating Android tasks and back stack

61,835 views

Published on

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

Published in: Technology, Education
15 Comments
129 Likes
Statistics
Notes
No Downloads
Views
Total views
61,835
On SlideShare
0
From Embeds
0
Number of Embeds
546
Actions
Shares
0
Downloads
1,123
Comments
15
Likes
129
Embeds 0
No embeds

No notes for slide

Manipulating Android tasks and back stack

  1. 1. Mastering Android Tasks
  2. 2. 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.
  3. 3. Activities Single UI screen. Stacked like a deck of cards. Only one is visible. Only one is active. New Activities are places on top.
  4. 4. Activities Application may have more than one Activity
  5. 5. 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 C Activity B Activity A
  6. 6. Tasks Each app has at least one task May have more Tasks can be moved to “background” Keeping their back stack. User navigate between tasks Easy switching between apps. Multitasking experience.
  7. 7. User's Action Home (Foreground Task) (Backgorund Tasks)
  8. 8. User's Action Home Ingress (Foreground Task) (Backgorund Tasks)
  9. 9. User's Action Home Ingress Home (Foreground Task) (Backgorund Tasks)
  10. 10. User's Action Home Ingress Home G+ (Foreground Task) (Backgorund Tasks)
  11. 11. User's Action Home Ingress Home G+ Enters Post (Foreground Task) (Backgorund Tasks)
  12. 12. User's Action Home Ingress Home G+ Enters Post Home (Foreground Task) (Backgorund Tasks)
  13. 13. User's Action Home Ingress Home G+ Enters Post Home Flipboard (Foreground Task) (Backgorund Tasks)
  14. 14. User's Action Home Ingress Home G+ Enters Post Home Flipboard Home (Foreground Task) (Backgorund Tasks)
  15. 15. User's Action Home Ingress Home G+ Enters Post Home Flipboard Home Recent Apps (Foreground Task) (Backgorund Tasks)
  16. 16. User's Action Home Ingress Home G+ Enters Post Home Flipboard Home Recent Apps G+ (Foreground Task) (Backgorund Tasks)
  17. 17. Why Manipulate?
  18. 18. Why Manipulate default behavior? Multiple entry points to the app Launcher Notification Share Intent “Singleton” Activity Activity that wants to share data between instances. (example: browser)
  19. 19. Changing default behavior <activity> tag attributes in AndroidManifest.xml Launchmode TaskAffinity ... Adding flags to Intent used to launch the activity FLAG_ACTIVITY_NEW_TASK FLAG_ACTIVITY_CLEAR_TOP FLAG_ACTIVITY_SINGLE_TOP Warning – only sounds easy.
  20. 20. 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)
  21. 21. Example Manifest: Activity A is launched
  22. 22. Example Manifest: Activity A is launched Activity A Com.myApp
  23. 23. Example Manifest: Activity A is launched Activity A's code: Intent I = new Intent (this,ActvitiyB.class) startActvitiy(i) Activity A Com.myApp
  24. 24. Example Manifest: Activity A is launched Activity A's code: Intent I = new Intent (this,ActvitiyB.class) startActvitiy(i) Activity B Activity A Com.myApp
  25. 25. What?
  26. 26. 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
  27. 27. Example Manifest: Activity A is launched Activity A Com.myApp
  28. 28. Example Manifest: Activity A is launched Activity A's code: Intent I = new Intent (this,ActvitiyB.class) I.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) startActvitiy(i) Activity A Com.myApp
  29. 29. Example Manifest: Activity A is launched Activity A's code: Intent I = new Intent (this,ActvitiyB.class) I.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) startActvitiy(i) Activity A Com.myApp Activity B Com.something
  30. 30. Android:launchMode Specifies how the Activity should be launched Four different modes: Standard (default) Single Top Single Task Single Instance
  31. 31. 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
  32. 32. Example Manifest: Activity A is launched Activity A Com.myApp
  33. 33. Example Manifest: Activity A is launched Activity A launches Activity B (no flags) Activity A Com.myApp
  34. 34. Example Manifest: Activity A is launched Activity A launches Activity B (no flags) Activity B Activity A Com.myApp
  35. 35. Example Manifest: Activity A is launched Activity A launches Activity B (no flags) Activity B launches Activity A Activity B Activity A Com.myApp
  36. 36. Example Manifest: Activity A is launched Activity A launches Activity B (no flags) Activity B launches Activity A Activity A Activity B Activity A Com.myApp
  37. 37. Example Manifest: 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 A Activity B Activity A Com.myApp
  38. 38. Example Manifest: 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 A Activity B Activity A Com.myApp Activity B Com.something
  39. 39. Example Manifest: 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 Activity B Activity A Com.myApp Activity B Com.something
  40. 40. Example Manifest: 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 Activity B Activity A Activity A Activity B Com.myApp Com.something
  41. 41. 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
  42. 42. Example Manifest: Activity A is launched Activity A Com.myApp
  43. 43. Example Manifest: Activity A is launched User pulls the notification bar and clicks on notification that fires Activity intent to Activity A Activity A Com.myApp
  44. 44. Example Manifest: Activity A is launched User pulls the notification bar and clicks on notification that fires Activity intent to Activity A OnNewIntent(..) Activity A Com.myApp
  45. 45. Example Manifest: 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) OnNewIntent(..) Activity A Com.myApp
  46. 46. Example Manifest: 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 Activity A Com.myApp
  47. 47. Example Manifest: 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 Activity B Activity A Com.myApp
  48. 48. Example Manifest: 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 Activity A Activity B Activity A Com.myApp
  49. 49. 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
  50. 50. Example Manifest: Activity A is launched Activity A Com.myApp
  51. 51. Example Manifest: Activity A is launched Acitivity A launches Activity B Activity A Com.myApp
  52. 52. Example Manifest: Activity A is launched Acitivity A launches Activity B Activity B Activity A Com.myApp
  53. 53. Example Manifest: Activity A is launched Acitivity A launches Activity B Activity B launches Activity C with FLAG_NEW_TASK Activity B Activity A Com.myApp
  54. 54. Example Manifest: Activity A is launched Acitivity A launches Activity B Activity B launches Activity C with FLAG_NEW_TASK Activity B Activity A Com.myApp Activity C Com.something
  55. 55. Example Manifest: Activity A is launched Acitivity A launches Activity B Activity B launches Activity C with FLAG_NEW_TASK Activity C launches Activity A Activity B Activity A Com.myApp Activity C Com.something
  56. 56. Example Manifest: Activity A is launched Acitivity A launches Activity B Activity B launches Activity C with FLAG_NEW_TASK Activity C launches Activity A OnNewIntent(..) Activity A Com.myApp Activity C Com.something
  57. 57. 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
  58. 58. Example Manifest: Activity A is launched Activity A Com.myApp
  59. 59. Example Manifest: Activity A is launched Activity A launches Activity B Activity A Com.myApp
  60. 60. Example Manifest: Activity A is launched Activity A launches Activity B Activity A Com.myApp Activity B Com.myApp
  61. 61. Example Manifest: Activity A is launched Activity A launches Activity B Activity B launches Activity C Activity A Com.myApp Activity B Com.myApp
  62. 62. Example Manifest: Activity A is launched Activity A launches Activity B Activity B launches Activity C Activity C Activity A Com.myApp Activity B Com.myApp
  63. 63. Example Manifest: Activity A is launched Activity A launches Activity B Activity B launches Activity C Activity C launches Activity A Activity C Activity A Com.myApp Activity B Com.myApp
  64. 64. Example Manifest: Activity A is launched Activity A launches Activity B Activity B launches Activity C Activity C launches Activity A Activity C OnNewIntent(..) Activity A Com.myApp Activity B Com.myApp
  65. 65. 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().
  66. 66. Example Manifest: Activity A is launched Activity A Com.myApp
  67. 67. Example Manifest: Activity A is launched Activity A launches Activity B Activity A Com.myApp
  68. 68. Example Manifest: Activity A is launched Activity A launches Activity B Activity B Activity A Com.myApp
  69. 69. Example Manifest: Activity A is launched Activity A launches Activity B Activity B launches Activity C with FLAG_NEW_TASK Activity B Activity A Com.myApp
  70. 70. Example Manifest: Activity A is launched Activity A launches Activity B Activity B launches Activity C with FLAG_NEW_TASK Activity B Activity A Com.myApp Activity C Com.something
  71. 71. Example Manifest: Activity A is launched Activity A launches Activity B Activity B launches Activity C with FLAG_NEW_TASK Activity C launches Activity D Activity B Activity A Com.myApp Activity C Com.something
  72. 72. Example Manifest: Activity A is launched Activity A launches Activity B Activity B launches Activity C with FLAG_NEW_TASK Activity C launches Activity D Activity B Activity D Activity A Activity C Com.myApp Com.something
  73. 73. Example Manifest: 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 Activity D Activity A Activity C Com.myApp Com.something
  74. 74. Example Manifest: 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 Activity B Activity D Activity A Activity C Com.myApp Com.something
  75. 75. Example Manifest: Activity A is launched Activity A launches Activity B Activity B launches Activity C with FLAG_NEW_TASK Activity C launches Activity D onResume() User switches to com.myApp Activity B launches Activity C with FLAG_NEW_TASK Activity B Activity D Activity A Activity C Com.myApp Com.something
  76. 76. Easy – but may be tricky
  77. 77. Example Manifest: Activity A is launched Activity A Com.myApp
  78. 78. Example Manifest: Activity A is launched Activity A launches Activity B Activity A Com.myApp
  79. 79. Example Manifest: Activity A is launched Activity A launches Activity B Activity B Activity A Com.myApp
  80. 80. Example Manifest: Activity A is launched Activity A launches Activity B Activity B launches Activity C with FLAG_NEW_TASK Activity B Activity A Com.myApp
  81. 81. Example Manifest: Activity A is launched Activity A launches Activity B Activity B launches Activity C with FLAG_NEW_TASK Activity B Activity A Com.myApp Activity C Com.something
  82. 82. Example Manifest: Activity A is launched Activity A launches Activity B Activity B launches Activity C with FLAG_NEW_TASK Activity C launches Activity D Activity B Activity A Com.myApp Activity C Com.something
  83. 83. Example Manifest: Activity A is launched Activity A launches Activity B Activity B launches Activity C with FLAG_NEW_TASK Activity C launches Activity D Activity B Activity D Activity A Activity C Com.myApp Com.something
  84. 84. Example Manifest: 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 Activity D Activity A Activity C Com.myApp Com.something
  85. 85. Example Manifest: 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 Activity B Activity D Activity A Activity C Com.myApp Com.something
  86. 86. Example Manifest: 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 Activity D Activity B Activity D Activity A Activity C Com.myApp Com.something
  87. 87. Example Manifest: 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 Activity D Activity B Activity D Activity A Activity C Com.myApp Com.something
  88. 88. New instance of D created!
  89. 89. 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.
  90. 90. Example Manifest: Activity A is launched Activity A Com.myApp
  91. 91. Example Manifest: Activity A is launched Activity A launches Activity B Activity A Com.myApp
  92. 92. Example Manifest: Activity A is launched Activity A launches Activity B Activity B Activity A Com.myApp
  93. 93. Example Manifest: Activity A is launched Activity A launches Activity B Activity B launches Activity C with FLAG_NEW_TASK Activity B Activity A Com.myApp
  94. 94. Example Manifest: Activity A is launched Activity A launches Activity B Activity B launches Activity C with FLAG_NEW_TASK Activity B Activity A Com.myApp Activity C Com.something
  95. 95. Example Manifest: 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() Activity B Activity A Com.myApp Activity C Com.something
  96. 96. Example Manifest: 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() Activity B Activity A Com.myApp Activity C D Com.something
  97. 97. Example Manifest: 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 Activity A Com.myApp Activity C D Com.something
  98. 98. Example Manifest: 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 Activity B Activity A Com.myApp Activity C D Com.something
  99. 99. Example Manifest: 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 onResume() Activity B Activity A Com.myApp Activity C D Com.something
  100. 100. Asked for Activity C - > Got activiy D.
  101. 101. It is not over yet...
  102. 102. Example Manifest: Activity A is launched Activity A Com.myApp
  103. 103. Example Manifest: Activity A is launched Activity A launches Activity B with startActivityForResult() Activity A Com.myApp
  104. 104. Example Manifest: Activity A is launched Activity A launches Activity B with startActivityForResult() OnActivityResult(...) Activity A Com.myApp
  105. 105. Example Manifest: Activity A is launched Activity A launches Activity B with startActivityForResult() Activity A Com.myApp Activity B Com.myApp
  106. 106. StartActivityForResult If an activity calls startActivityForResult and new task is created – onActivityResult is called immediately. “its not bug – its a feature” approach.
  107. 107. 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.
  108. 108. Questions? ran@mobiliup.com +Ran Nachmany

×