Android activity launch modes and tasks Gonçalo Silva / Novoda
 
<ul><li>Co-organizer </li></ul><ul><li>~1250 members </li></ul>
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 = &quot;1.0&quot;   encoding = &quot;utf-8&quot; ?> < manifest  xmlns:android = &quot...
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 <ul><li>Action </li></ul><ul><li>Data – URI and/or MIME type </li></ul><ul><li>Category </li></ul><ul><li>Ex...
Explicit intent resolution Intent intent =  new  Intent( this , HelloWorldActivity. class ); startActivity(intent);
Implicit intent resolution Uri google = Uri. parse ( &quot;http://www.google.com&quot; ); Intent intent =  new  Intent( In...
Intent filter < activity   android:name = &quot;.SimpleBrowser&quot;   > < intent-filter  > < action   android:name = &quo...
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 <ul><li>standard </li></ul><ul><li>singleTop </li></ul><ul><li>singleTask </li></ul><ul><li>singleInstan...
Defined in the manifest < activity android:launchMode = &quot;singleTop&quot; android:name = &quot;.HelloWorldActivity&quo...
Set as a flag in an intent Intent intent =  new  Intent( this , HelloWorldActivity. class ); intent.addFlags(Intent. FLAG_...
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 ...
singleTask Created at the root of a new task Only one instance of the activity can exist at a time onNewIntent() is called...
singleInstance Like singleTop but the only member of its task Any activities started will open in a separate task.
Examples of launchModes <ul><li>singleTop: Dashboard, Search </li></ul><ul><li>singleTask: Browser, Google Maps </li></ul>...
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_RESET FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
Other manifest attributes to help deal with activities and tasks
android:taskAffinity <ul><li>Useful to ensure certain activities are always in the same task </li></ul><ul><li>By default,...
android:allowTaskReparenting <ul><li>Next time an activity comes to the front, move it to a task it has an affinity for </...
android:clearTaskOnLaunch <ul><li>Clear all activities from the task except the root, when launching the app from the home...
android:alwaysRetainTaskState <ul><li>The state of the task is mantained by the system, even after a long period of time <...
android:finishOnTaskLaunch <ul><li>Shut down an instance of an activity when its task is resumed </li></ul><ul><li>Trumps ...
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: <ul><li>Activities and how they live in tasks </li></ul><ul><li>Intents and re-using activities in other apps </...
Thank you for listening! Any Questions?
Upcoming SlideShare
Loading in...5
×

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

12,839

Published on

2011-11-02 | 03:45 PM - 04:35 PM |
Android Activities can possess one of four launch modes and one of many activity tasks. We will explore how your choices of launch modes and tasks affect your Applications back stack history and what will happen behind the scenes. After this talk you will confidently be able to wield the best launch modes for your apps activities in every situation!

1 Comment
10 Likes
Statistics
Notes
  • Nice
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
12,839
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
269
Comments
1
Likes
10
Embeds 0
No embeds

No notes for slide

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

  1. 1. Android activity launch modes and tasks Gonçalo Silva / Novoda
  2. 3. <ul><li>Co-organizer </li></ul><ul><li>~1250 members </li></ul>
  3. 4. Activity launch modes and tasks..
  4. 5. What is an Activity?
  5. 6. Main building block of Android applications
  6. 7. HelloWorldActivity.java public class HelloWorldActivity extends Activity { .... }
  7. 8. AndroidManifest.xml <? xml version = &quot;1.0&quot; encoding = &quot;utf-8&quot; ?> < manifest xmlns:android = &quot;http://schemas.android.com/apk/res/android&quot; package = &quot;com.novoda.jax&quot; android:versionCode = &quot;1&quot; android:versionName = &quot;1.0&quot; > < application > < activity android:name = &quot;.HelloWorldActivity&quot; /> </ application > </ manifest >
  8. 9. Activity task Sequence of related activities a user follows
  9. 14. Activity stack / back stack
  10. 15. How are activites started?
  11. 16. Messaging facility for late run-time binding between components Intents!
  12. 17. Intent object <ul><li>Action </li></ul><ul><li>Data – URI and/or MIME type </li></ul><ul><li>Category </li></ul><ul><li>Extras – key/value pairs </li></ul><ul><li>Component name </li></ul><ul><li>Flags </li></ul>
  13. 18. Explicit intent resolution Intent intent = new Intent( this , HelloWorldActivity. class ); startActivity(intent);
  14. 19. Implicit intent resolution Uri google = Uri. parse ( &quot;http://www.google.com&quot; ); Intent intent = new Intent( Intent. ACTION_VIEW , google); startActivity(intent);
  15. 20. Intent filter < activity android:name = &quot;.SimpleBrowser&quot; > < intent-filter > < action android:name = &quot;android.intent.action.VIEW&quot; /> < category android:name = &quot;android.intent.category.DEFAULT&quot; /> < data android:scheme = &quot;http&quot; /> </ intent-filter > </activity>
  16. 21. Intents are great! Loosely coupled activities Specify your behaviour as intentions
  17. 22. Activity re-use Starting an activity in a different application
  18. 23. Task Twitter
  19. 24. Task Twitter
  20. 25. Task Twitter Gmail
  21. 26. Activity re-use Some activities are started in a different task
  22. 27. Task A Gmail
  23. 28. Task A Gmail Task B Browser
  24. 29. Activity re-use Allows user to resume after leaving original task Why different tasks?
  25. 30. How can we resume an activity?
  26. 32. Resuming an activity
  27. 33. Resuming a “killed” activity
  28. 34. How can we choose a specific task behaviour for our activities?
  29. 35. Activity launch modes Define how a new instance of an activity is associated with the current task
  30. 36. Four launch modes <ul><li>standard </li></ul><ul><li>singleTop </li></ul><ul><li>singleTask </li></ul><ul><li>singleInstance </li></ul>
  31. 37. Defined in the manifest < activity android:launchMode = &quot;singleTop&quot; android:name = &quot;.HelloWorldActivity&quot; />
  32. 38. Set as a flag in an intent Intent intent = new Intent( this , HelloWorldActivity. class ); intent.addFlags(Intent. FLAG_ACTIVITY_SINGLE_TOP ); startActivity(intent);
  33. 39. Demo App!!! Search the market for: launchmode
  34. 40. standard – default launch mode
  35. 41. 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
  36. 42. 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
  37. 43. singleInstance Like singleTop but the only member of its task Any activities started will open in a separate task.
  38. 44. Examples of launchModes <ul><li>singleTop: Dashboard, Search </li></ul><ul><li>singleTask: Browser, Google Maps </li></ul><ul><li>singleInstance: Home screen </li></ul>
  39. 45. Intent flags and launchModes
  40. 46. FLAG_ACTIVITY_SINGLE_TOP
  41. 47. FLAG_ACTIVITY_MULTIPLE_TASK FLAG_ACTIVITY_NEW_TASK Force an activity to a new task Not recommended!
  42. 48. FLAG_ACTIVITY_NO_HISTORY
  43. 49. FLAG_ACTIVITY_CLEAR_TOP
  44. 50. FLAG_ACTIVITY_REORDER_TO_FRONT
  45. 51. FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS The new activity is not kept in the list of recently launched activities
  46. 52. FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
  47. 53. Other manifest attributes to help deal with activities and tasks
  48. 54. android:taskAffinity <ul><li>Useful to ensure certain activities are always in the same task </li></ul><ul><li>By default, all activities in an application have the same affinity </li></ul><ul><li>singleTask Gotcha!!! </li></ul>
  49. 55. android:allowTaskReparenting <ul><li>Next time an activity comes to the front, move it to a task it has an affinity for </li></ul><ul><li>Eg: email and browser </li></ul>
  50. 56. android:clearTaskOnLaunch <ul><li>Clear all activities from the task except the root, when launching the app from the home screen </li></ul><ul><li>Only honoured for activities that start a new task </li></ul><ul><li>Can be combined with “allowTaskReparenting” </li></ul>
  51. 57. android:alwaysRetainTaskState <ul><li>The state of the task is mantained by the system, even after a long period of time </li></ul><ul><li>Only honoured for activities that start a new task </li></ul><ul><li>Eg: browser tabs </li></ul>
  52. 58. android:finishOnTaskLaunch <ul><li>Shut down an instance of an activity when its task is resumed </li></ul><ul><li>Trumps android:alwaysRetainTaskState </li></ul>
  53. 59. singleTask and singleInstance change the UI flow Use with caution!
  54. 60. singleTask UI flow
  55. 64. singleInstance UI flow
  56. 70. Two launcher icons
  57. 71. Provide a launcher icon for each singleTask / singleInstance activity
  58. 72. Another disadvantage: startActivityForResult()
  59. 73. Recommended launchModes: standard and singleTop with intent flags combinations will suit most use cases
  60. 74. To recap: <ul><li>Activities and how they live in tasks </li></ul><ul><li>Intents and re-using activities in other apps </li></ul><ul><li>Activity lifecycle and the four launchmodes </li></ul><ul><li>Intent flags and manifest attributes </li></ul><ul><li>singleTask and singleInstance disadvantages </li></ul>
  61. 75. Thank you for listening! Any Questions?
  1. ¿Le ha llamado la atención una diapositiva en particular?

    Recortar diapositivas es una manera útil de recopilar información importante para consultarla más tarde.

×