Launch mode for Android activities

There are a total of 4 launch modes. They are standard, singleTop, singleTask and singleInstance. You can select the launch mode by specifying the android:launchMode attribute in the <activity> tag in AndroidMainfest.xml.

1.standard

standard is the default startup mode for activities, and all activities will automatically use this startup mode unless explicitly specified.

Android uses the back stack to manage activities. In standard mode, whenever a new activity is started, it will be pushed into the back stack and placed at the top of the stack. For activities using standard mode, the system will not Regardless of whether the activity already exists on the back stack, each launch creates a new instance of the activity.


2.singleTop

When the startup mode of the activity is specified as singleTop, if it is found that the top of the return stack is already the activity when the activity is started, it is considered that it can be used directly, and no new activity instance will be created.

However, when the activity to be started is not at the top of the stack, a new instance will still be created when the activity is started.


3.singleTask

Using the singleTop mode can solve the problem of repeatedly creating activities at the top of the stack, but if the activity is not at the top of the stack, multiple activity instances may still be created.

   When the startup mode of the activity is specified as singleTask, the system will first check whether there is an instance of the activity in the return stack every time the activity is started . All are popped from the stack, and if nothing is found, a new instance will be created.


4,singleInstance

The singleInstance mode is the most complex and special of the 4 launch modes. An activity in this mode will enable a new back stack to manage the activity.

    Suppose there is an activity in our program that allows other programs to call, if other programs share an instance of this activity with our program, how should we implement it? It is impossible to use the first three startup modes, because each program has its own Return stack, the same activity must create a new instance when different return stacks are pushed onto the stack. This problem can be solved by using the singleInstance mode. In this mode, there will be a separate return stack to manage this activity. No matter which application accesses this activity, it shares the same return stack, which solves the problem of shared instances.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325807675&siteId=291194637