Managing Activity Backstack


Published on

This Presentation talks about various aspects of managing the Activity Backstack.
Please note this was prepared last year but most of the concepts are still relevant

Published in: Technology

Managing Activity Backstack

  1. 1. Managing Android Back stack Rajdeep Dua June 2012Monday, March 18, 13
  2. 2. Agenda • Activity Back Stack explained • Tasks • Bringing activities to the foreground • Modifying Activity launch behavior using manifest file attributes • Other advanced behaviors • No History • Task Reset • Task AffinityMonday, March 18, 13
  3. 3. Android Back Stack • An Android application is composed of multiple activities • New Activities are launched using Intents • Older ones are either destroyed or placed on a back stackMonday, March 18, 13
  4. 4. Android Back Stack • New Activity can be launched in the same task or a new task • Back stack is a data structure which holds activities in the background which user can navigate to using back button. • Back Stack works in the last in first out mode. • It can be cleared in case of low memoryMonday, March 18, 13
  5. 5. Tasks • Task is a collection of activities that user performs while doing a job • An activity is launched in a new task from the Home screen • By default all the activities in an application belong to the same task • Activities in a task can exist in the foreground, background as well as on the back stack.Monday, March 18, 13
  6. 6. Tasks Activity A and Activity B are launched in Task A. By default all activities in the same application belong to a single task.Monday, March 18, 13
  7. 7. Bringing Activities to the Foreground • Activities pop back to foreground as the user presses back button • As an example in the figure • Activity B gets destroyed as soon as user presses back button • Activity A comes to the foregroundMonday, March 18, 13
  8. 8. Task in the Background • Application 1 Launches in Task A • Home Screen Task A Goes to the Background • Application 2 Launches in Task B in the foregroundMonday, March 18, 13
  9. 9. Modifying Activity Launch Behavior using Manifest FlagsMonday, March 18, 13
  10. 10. Activity Launch Mode • Activities can be configured to launch with the following modes in the Manifest file Use Case Launch Mode standard Normal singleTop singleTask Specialized singleInstanceMonday, March 18, 13
  11. 11. Activity Launch Mode standard <activity android: launchMode=”standard”/> • Default Launch Mode • All the Activities belong to the same task in the applicationMonday, March 18, 13
  12. 12. Activity Launch Mode singleTop <activity android:launchMode=”singleTop”/> • Single instance of an Activity can exist at the top of the back stack • Multiple instances can exist if there are activities between themMonday, March 18, 13
  13. 13. Activity Launch Mode singleTask <activity android:launchMode=”singleTask”/> • Activity is the root of a task • Other activities are part of this task if they are launched from this activity • If this activity already exists Intent is routed to that InstanceMonday, March 18, 13
  14. 14. Activity Launch Mode singleTask...contd • If this activity already exists Intent is routed to that instance and onNewIntent() Lifecycle method is calledMonday, March 18, 13
  15. 15. Activity Launch Mode singleInstance <activity android:launchMode=”singleInstance”/> • Activity B is launched with launchMode singleInstance, it is always the only activity of its taskMonday, March 18, 13
  16. 16. SingleTop using Intent Flags • Single Instance of an Activity at the top of the back stack can also be achieved using Intent Flags. Step 1 : Create Activity A using an Intent with no flag Intent intentA = new Intent(MainActivity.this, ActivityA.class); startActivity(intentA); Step 2 : Create Activity A again with the Intent flag FLAG_ACTIVITY_SINGLE_TOP Intent intentA = new Intent(MainActivity.this, ActivityA.class); intentA.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP) startActivity(intentA);Monday, March 18, 13
  17. 17. Other Behaviors.. • No History on the Back stack • Task Reset and clearing the Back Stack • Task Affinity attribute in ManifestMonday, March 18, 13
  18. 18. No History on the Back Stack • An Activity can be set to have no history on the back stack - it will be destroyed as soon as user moves away from it using the the intent flag FLAG_ACTIVITY_NO_HISTORYMonday, March 18, 13
  19. 19. Clear Activity on Task Reset • Activities can be cleared from the back stack when a task is reset. • Use the Intent Flags FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET and FLAG_ACTIVITY_RESET_TASK_IF_NEEDED are used together to clear activities on a task reset. • Flag FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET  to indicate that the activity will be destroyed when the task is reset. • Flag FLAG_ACTIVITY_RESET_TASK_IF_NEEDED refers to the activity in the back stack beyond which activities are destroyed.Monday, March 18, 13
  20. 20. Clear Activity on Task Reset ..contdMonday, March 18, 13
  21. 21. Clear Activity on Task Reset Sample Code //Code below shows how the Activity A gets launched with the appropriate flags. Intent intentA = new Intent(MainActivity.this, ActivityA.class); intentA.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) ; startActivity(intentA); //sample code below shows the flag with with Activity B and Activity C get launched. Intent intentB = new Intent(ActivityA.this, ActivityB.class); intentB.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET ); startActivity(intentB);Monday, March 18, 13
  22. 22. TaskAffinity • Task Affinity can be used to launch activities in a new task, e.g having two Launcher Activities which need to have their own task • EntryActivityA and EntryActivityB will Launch in their own tasksMonday, March 18, 13
  23. 23. TaskAffinity ..contd • EntryActivityA and EntryActivityB will Launch in their own tasks • Manifest file entries for the two activities <activity android:name=".EntryActivityA" android:label="@string/activitya" android:taskAffinity="stacksample.activitya"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".EntryActivityB" android:label="@string/activityb" android:taskAffinity="stacksample.activityb"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>Monday, March 18, 13
  24. 24. Summary • Default behavior of using the back stack serves most of the use cases • Default behavior can be modified using Manifest file attributes and Intent flags to launch activities in their own task, having a single instance or having no historyMonday, March 18, 13