Four startup modes of Activity and onNewIntent()

Detailed explanation of Activity startup mode in Android
  In Android, each interface is an Activity, and the switching interface operation is actually an instantiation operation between multiple different Activities. In Android, the startup mode of an Activity determines the startup mode of an Activity.

  There are four kinds of startup modes of Android total Activity:
quote

Activity launch mode settings:

        <activity android:name=".MainActivity" android:launchMode="standard" />

Four launch modes of Activity:

    1. Standard

        default launch mode, an Activity will be created every time an Activity is activated, and placed in in the task stack.

    2. singleTop

        If there is an instance of the Activity at the top of the stack of the task, it will reuse the instance, otherwise it will create a new instance and put it on the top of the stack (even if the Activity instance already exists in the stack, as long as it is not on the top of the stack, it will be create an instance).

    3.

        If the singleTask already has an instance of the Activity in the stack, it will reuse the instance (the instance's onNewIntent() will be called). Reuse will bring the instance back to the top of the stack, so the instances above it will be removed from the stack. If the instance does not exist on the stack, a new instance will be created and placed on the stack.

    4. singleInstance

        creates the Activity instance in a new stack, and allows multiple applications to share the Activity instance in the new stack. Once the instance of the Activity that changes the mode exists in a certain stack, any application will reuse the instance in the stack when the Activity is activated again. The effect is equivalent to that of multiple applications sharing an application. in application.

       Everyone encounters a situation where the Activity of an application is called and started in multiple ways, and multiple calls hope that only one instance of the Activity exists, which requires the onNewIntent (Intent intent) method of the Activity. Just add your own implementation of onNewIntent(intent) to the Activity and set lanuchMode="singleTask" to the Activity in the Manifest.

       onNewIntent() is very easy to use. When the Activity is first started, it executes onCreate()---->onStart()---->onResume() and other subsequent life cycle functions, which means that starting the Activity for the first time does not It will be executed to onNewIntent(). And if you want to start Activity later, it is to execute onNewIntent()---->onResart()------>onStart()----->onResume( ). If the android system releases the existing Activity due to insufficient memory, the Activity will be restarted when it is called again, that is, onCreate()---->onStart()---->onResume() and so on.

     When calling onNewIntent(intent), you need to use setIntent(intent) in onNewIntent() to assign it to the Activity's Intent. Otherwise, subsequent getIntent() will get the old Intent.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327007980&siteId=291194637