[Points] Task and Back Stack
Task and Back Stack
- a new task is created and the "main" activity for that app opens as the root activity in the stack
- new activity is pushed on the top of the stack
- pressed back the top activity pop from stack
- no activity no task
- Home screen make task background
- one activity can get multiple instants(Figure 1)
Figure 1
- implememt onSaveInstanceState() save activity state because activity may be detroyed in backgroud
Managing Tasks
- android:taskAffinity( set activity belong to which task. as default all activity in same application using same taskAffinity )
- android:launchMode (standard,singleTop,singleTask,singleInstance)
- intent_filter (FLAG_ACTIVITY_NEW_TASK,FLAG_ACTIVITY_CLEAR_TOP,FLAG_ACTIVITY_SINGLE_TOP)
Caution: Most applications should not interrupt the default behavior for activities and tasks.
Defining launch modes
- using the manifest file
- using intent filter flag
- A start B ,Activity A's request (as defined in the intent) is honored over (以A为准?) Activity B's request (as defined in its manifest).
Note: Some launch modes available for the manifest file are not available as flags for an intent and, likewise, some launch modes available as flags for an intent cannot be defined in the manifest.
- the web browser activity should always open in its own task—by specifying the singleTask
- if you start an activity that specifies the singleTask launch mode, then if an instance of that activity exists in a background task, that whole task is brought to the foreground.(Figure 2)
Figure 2
Clear the back stack
- if the user leaves a task for a long time, the system clears the task of all activities except the root activity. When the user returns to the task again, only the root activity is restored.
- modify system clear task way
- alwaysRetainTaskState (retains all activities in its stack)
- clearTaskOnLaunch (clear all activities in its stack when user leaving this stack)
- finishOnTaskLaunch (clear just this activity when user leaving its stack)
Starting a task
- stark task with "android.intent.action.MAIN" as the specified action and "android.intent.category.LAUNCHER" as the specified category(in AndroidManifest)