Four startup modes of Activity in Android and onNewIntent of

  • 2021-10-13 08:43:56
  • OfStack

Write at the front

Activity is one of the major components of Android4, which is used to interact directly with users. This article will introduce the startup process of Activity. There are roughly two ways for users to start Activity: one is to click the icon of the application on the desktop and enter the main interface of the application; The other is to enter a new Activity in the application. In the former, the desktop is actually the interface for the system to apply launcher. Clicking the application icon will make the main interface of the application, which is essentially from Activity of one application to Activity of another application.

Therefore, whether you enter the application main interface from the desktop or enter a new Activity in the application, you will eventually call the Activity $startActivity method.

It is worth mentioning that the source code for starting Activity in Android 5.0, 7.0 and other versions is slightly different. The upgrade of the version only encapsulates the code, and eventually the task of starting Activity will be handed over to ApplicationThread.

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 several different Activity. In Android, the startup mode of Activity determines the startup operation mode of Activity.

The startup modes of Android total Activity are divided into four types:

Activity Startup Mode Settings:

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

Four startup modes of Activity:

1. standard

Default startup mode, Activity is created each time Activity is activated and placed in the task stack.

2. singleTop

If an instance of the Activity exists at the top of the task's stack, the instance is reused, or a new instance is created and placed at the top of the stack (even if the Activity instance already exists in the stack, an instance is created as long as it is not at the top of the stack).

3. singleTask

If an instance of the Activity already exists in the stack, the instance is reused (the instance's onNewIntent () is called). When reused, the instance is returned to the top of the stack, so the instance above it will be removed from the stack. If the instance does not exist in the stack, a new instance will be created and put on the stack.

4. singleInstance

The Activity instance is created in a new stack, and the Activity instance in the modified stack is shared by multiple applications. The instance of the modified Activity exists in a stack. When any application activates the modified Activity, it will reuse the instance in the stack. The effect is equivalent to multiple applications sharing one application. No matter who activates the modified Activity, they will enter the same application

Everyone encountered an application of Activity for a variety of ways to call start, multiple calls hope that only one instance of Activity exists, which requires Activity onNewIntent(Intent intent) Method. Just add your own in Activity onNewIntent(intent) Plus setting lanuchMode= "singleTask" for Activity in Manifest.

onNewIntent () is very easy to use, and onCreate () is executed when Activity is started first-- > onStart()---- > onResume () and other subsequent life cycle functions, that is to say, the first time you start Activity will not be executed to onNewIntent (). And if you want to start Activity again, it is to execute onNewIntent ()---- > onResart()------ > onStart()----- > onResume (). If the android system releases the existing Activity due to insufficient memory, it will restart Activity and execute onCreate () when calling again-- > onStart()---- > onResume (), etc.

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

Summarize


Related articles: