Write in front
Activity is one of the four major components of Android, used to interact directly with users. This article will introduce the startup process of Activity. There are roughly two ways for users to start an activity: one is to click the application icon on the desktop to enter the application's main interface; the other is to enter a new activity in the application. In the former, the desktop is actually the interface of the system application launcher. Clicking on the application icon will launch the main interface of the application. In essence, it is from the activity of one application to the activity of another application.
Therefore, whether you enter the main interface of the application from the desktop or enter a new Activity in the application, the Activity$startActivity method will eventually be called.
It is worth mentioning that the source code of launching Activity in Android 5.0, 7.0 and other versions is a bit different. The upgrade of the version only encapsulates the code, and in the end, the task of launching Activity will be handed over to ApplicationThread for processing.
Detailed explanation of Activity startup mode in Android
In Android, each interface is an Activity, and switching interface operations are actually instantiated operations between multiple different activities. The startup mode of Activity in Android determines the startup and operation of Activity.
The startup mode of Android total activity is divided into four types. Let’s see which four types are:
Activity startup mode settings:
<activity android:name=".MainActivity" android:launchMode="standard" />
Four startup modes of the activity:
1. standard
The default startup mode is created every time the Activity is activated and placed into the task stack.
2. singleTop
If an instance of the Activity happens to exist at the top of the stack of the task, the instance will be reused, otherwise a new instance will be created and placed on the top of the stack (even if the Activity instance already exists on the stack, an instance will be created as long as it is not on the top of the stack).
3. singleTask
If there is an instance of the Activity on the stack, reuse the instance (onNewIntent() of the instance will be called). When reused, the instance will be returned to the top of the stack, so the instance on 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
Create the Activity instance in a new stack and let multiple applications share the Activity instance in the stack. Once an instance of the activity in the mode exists in a certain stack, any application will reuse the instance in the stack when it reactivates the activity. The effect is equivalent to multiple applications sharing an application. No matter who activates the activity, it will enter the same application.
Everyone encounters the situation where an application's Activity is called for multiple calls and started. Multiple calls hope that only one Activity instance exists, which requires Activity'sonNewIntent(Intent intent)
The method is here. Just add your own in the ActivityonNewIntent(intent)
The implementation is also possible with the setting of lanuchMode="singleTask" for Activity in Manifest.
OnNewIntent() is very useful. When the activity is first started, it executes subsequent life cycle functions such as onCreate()---->onStart()---->onResume(), which means that the first time you start the Activity, it will not execute onNewIntent(). And if you want to start the Activity later, it is to execute onNewIntent()---->onResart()------>onStart()----->onResume(). If the Android system releases the existing Activity due to insufficient memory, then the Activity will be restarted when it is called again, that is, onCreate()---->onStart()---->onResume(), etc.
When onNewIntent(intent), you need to use setIntent(intent) to assign value to the Activity's Intent in onNewIntent(). Otherwise, subsequent getIntent() will get the old Intent.
Summarize
The above are the four startup modes and onNewIntent() of Activity in Android introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!