[Android development] Activity 4 loading methods

You can specify the android: launchMode attribute when configuring the Activity to configure the startup mode of the Activity.
This attribute supports 4 attribute values:

  • standard: standard mode, the default loading mode
  • singleTop: Task singleton stack top mode
  • singleTask: Singleton mode within Task
  • singleInstance: global singleton mode

standard

Every time the target Activity is started in standard mode, Android will always create a new instance for the target Activity and add the Activity to the current Task stack.
This mode will not start a new Task, and the new Activity will be added to the original Task.

singleTop

The singleTop mode is basically the same as the standard mode, but there is one difference: when the target Activity to be started is already at the top of the stack, the system will not recreate the target Activity instance, but directly reuse the existing Activity instance.
If the target Activity to be started is not at the top of the Task stack, the system will recreate the instance of the target Acitvity and add it to the top of the stack.

singleTask

To ensure that there is only one instance in the same Task, there are three situations:

  • If the target Activity to be started does not exist, the system will create an instance of the target Activity and add it to the top of the Task stack.
  • If the target Activity to be started is already at the top of the Task stack, it is the same as SingleTop at this time.
  • If the activity to be started already exists, but is not on the top of the stack, the system will remove all the activities on all the activities above the activity from the Task stack, so that the target activity is transferred to the top of the stack.

singleInstance

It is guaranteed that no matter from which Task the target Activity is started, only one target Activity instance will be created, and a brand new Task stack will be used to load the Activity instance.

  • If the target Activity to be started does not exist, the system will first create a brand new Task, then create an instance of the target Activity, and add it to the top of the new Task stack.
  • If the target Activity to be started already exists, no matter which application or Task it is located in, the system will transfer the Task where the Activity is located to the foreground to display the Activity.

Loading Activity in the singleInstance mode is always at the top of the Task stack, and the Task where the loaded Activity is located only contains the Activity.

Guess you like

Origin blog.csdn.net/weixin_42020386/article/details/112799490