Android development-Activity startup mode, androidactivity
I don't know how to study hard, but I regret it. -- Persuasion
I spent the whole afternoon and evening learning the Activity startup mode. I thought this knowledge point was very simple, but during the learning process, I found that, the Startup Mode of the Activity is not as simple as you think. Let's take a look at the four startup modes of the Activity. If you have any questions, please leave a message. If you have any mistakes, please criticize and correct them, thank you.
There are four startup modes for an Activity.
1. standard
2. singleTop
3. singleTask
4. singleInstance
:
LaunchMode plays an important role in the jump process of multiple activities. It can decide whether to generate a new Activity instance and whether to reuse an existing Activity instance, whether to share a task with other Activity instances. Here is a brief introduction to the concept of a task. A task is an object with a stack structure. A task can manage multiple activities, start an application, and create a corresponding task.
The following describes the startup modes in sequence.
1. standard
The standard mode is the default startup mode of the Activity. When the launchMode of the activity is not configured, it starts in the standard mode,
The following uses an instance to explain the startup mode.
The FirstActivity code is as follows:
package com.example.activitylauchmodepractice;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class FirstActivity extends Activity {private Button btn_jumpToSecondActivity;private TextView tv_showViewClass;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_first);tv_showViewClass=(TextView) findViewById(R.id.tv_showViewClass);tv_showViewClass.setText(FirstActivity.this.toString());btn_jumpToSecondActivity=(Button) findViewById(R.id.btn_jumpToSecondActivity);btn_jumpToSecondActivity.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent=new Intent(FirstActivity.this,FirstActivity.class);startActivity(intent);}});}}
Interface after startup
The corresponding Task Stack is as follows:
On this basis, we click the button to start the Activity again. The interface is as follows:
The process of changing the Task Stack is as follows:
Click the button again to jump to the FirstActivity interface as follows:
The process of changing the Task Stack is as follows:
Now we can analyze it. In the above process, we click three buttons to instantiate three firstactivities.
This is the feature of the standard mode: No matter whether an instance exists in the task stack, it will instantiate an Activity.
When we click the return button, it will sequentially push the top Activity out of the stack. In the process above, three activities are instantiated. Therefore, we need to click the return button three times to exit the application.
2. singleTop
In the preceding example, we specify the attribute of FirstActivity as android: launchMode = "singleTop"
Interface after startup
The task stack is as follows:
Click the button to find that no matter how many times the interface is clicked, It is instantiated only once. At this time, the task site is always an Activity. At this time, you can click the return key once to exit the application.
This is only the case of one Activity. Let's talk about the situation of multiple activities.
The second SecondActivity code is as follows:
package com.example.activitylauchmodepractice;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class SecondActivity extends Activity {private Button btn_jumpToFirstActivity_;private TextView tv_showViewClass;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_second);tv_showViewClass=(TextView) findViewById(R.id.tv_showViewClass);tv_showViewClass.setText(SecondActivity.this.toString());btn_jumpToFirstActivity_=(Button) findViewById(R.id.btn_jumpToFirstActivity);btn_jumpToFirstActivity_.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent=new Intent(SecondActivity.this, FirstActivity.class);startActivity(intent);}});}}
Slightly modify the FirstActivity code.
btn_jumpToSecondActivity.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent=new Intent(FirstActivity.this,SecondActivity.class);startActivity(intent);}});
In the preceding Activity, the FirstActivity startup mode is singleTop, And the SecondActivity startup mode is the default standard. After preparation, perform the following operations:
The startup interface is as follows:
The task stack is as follows:
On this basis, we click a button as follows:
The changes to the task stack are as follows:
The page for clicking the button again in SecondActivity is as follows:
The changes to the task stack are as follows:
From the process above, we can see that the serial number of the FirstActivity that is redirected from SecondActivity to FirstActivity again is different, and a FirstActivity is re-generated.
SingleTop Mode features: When you jump from SecondActivity to FirstActivity, the system finds that there is a FirstActivity instance, but it is not at the top of the stack, so a new instance is generated. This is the characteristic of singleTop startup mode, that is, if a corresponding Activity instance is found at the top of the stack, it will be reused and no new instances will be generated, if there is no corresponding Activity at the top of the stack, one is instantiated.
This mode is basically the same as the standard mode, but it is a little different. When the Activity to be started is already at the top of the Task stack, the system will not recreate the instance of the target Activity, instead, the Activity at the top of the Task stack is reused directly.
3. singleTask (singleTask Mode)
Based on the above, we changed the FirstActivity startup mode to android: launchMode = "singleTask"
After the instance is started, click the jump button three times, as shown in.
In the above process, the serial number of FirstActivity remains unchanged, and the serial number of SecondActivity changes. This indicates that no new instance is generated when the SecondActivity jumps to FirstActivity, however, a new instance is generated when you jump from FirstActivity to SecondActivity.
The process of changing the Task Stack is as follows:
In the previous jump process, when you jump from SecondActivity to FirstActivity, you find that SecondActivity disappears. This is the characteristic of singleTask. In this jump process, existing FirstActivity instances are generated, so no new instances are generated, instead, all the Activity instances on the FirstActivity are pushed out of the stack, and the FirstActivity is changed to the top object of the stack, which is displayed before the screen.
SingleTask Mode features: If a corresponding Activity instance is found, all other Activity instances on the Activity instance are made out of the stack, making the Activity instance the top object of the stack, display to the front of the screen.
The Activity has only one instance in the same Task. When the system loads the Activity in singleTask mode, there are three situations:
(1) If the Activity to be started does not exist, the system will create the instance and add it to the top of the Task stack.
(2) If the Activity to be started already exists and there is a stack top, the behavior is the same as that in singleTop mode.
(3) If the Activity to be started exists but is not on the top of the stack, the system will remove all other activities on the Activity from the Task so that the target Activity is on the top of the stack.
4. singleInstance (Global Singleton Mode)
This mode is the most difficult to understand in the four modes, because it creates a new task stack and places the Activity in this stack, and ensure that other activities do not enter. Because this mode is complicated, let's first talk about its principle, and then further understand it based on the instance, if the user opens two application stacks, application 1 and Application 2, and application 1 and Application 2, for example, on the left side, Activity3 is enabled in Application 1, in this case, Application 1 and Application 2 will share the reference of Activity3,
Note: The reference to public Activity is based on the premise that LaunchMode = "singleInstance" is set for Activity in Application 2.
Because this mode is complex, we will give two different examples to illustrate different problems.
Example 1,
Or the two above activities. During the jump of FirstActivity and SecondActivity, we print the ID of the task stack of the two activities.
Modify the preceding two activities and change the SecondActivity startup mode to singleInstance.
tv_showViewClass=(TextView) findViewById(R.id.tv_showViewClass);
TV _showViewClass.setText ("current Activity:" + "\ n" + this. toString () + "\ n" + "Current TaskId:" + this. getTaskId ());
The following figure shows the page after startup and after clicking the jump button.
We found that the taskids of the two activities are different, indicating that these two activities are located in different task stacks. This confirms that a new task stack is created for SecondActivity. Some may ask, at this time, if you click the return button, how do they get out of the stack? For example, if we click the return button, its job stack changes as shown in Figure
If we click the button in SecondActivity to jump to FirstActivity, how can we exit the application? In this case, its job stack changes as follows:
The lower part of the figure shows the jump to FirstActivity again in SecondActivity. At this time, the system will generate a FirstActivity instance in the original stack structure, and then roll back twice. Note that it has not exited, but back to SecondActivity. Why? It is because when we jump from SecondActivity to FirstActivity, our starting point is changed to the stack structure where the SecondActivity instance is located. In this way, we need to "return" to this stack structure.
Because singleInstance is more complex, let's take another example of two applications. In order to be confused with the preceding example, we will re-write the two applications.
The first App has two Activity types: Activity1 and Activity.
There is an Activity2 In the second App. In this App, we start the intent activity of the first App.
The source code of the Activity of the first App is as follows:
Package com. example. activitylauchmodepractice; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView; public class Activity1 extends Activity {private Button btn_jumpToSecondActivity; private TextView TV _showViewClass; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_first); TV _showViewClass = (TextView) findViewById (R. id. TV _showViewClass); TV _showViewClass.setText ("current Activity:" + "\ n" + this. toString () + "\ n" + "Current TaskId:" + this. getTaskId (); btn_jumpToSecondActivity = (Button) findViewById (R. id. btn_jumpToSharedActivity); btn_jumpToSecondActivity.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {Intent intent = new Intent (Activity1.this, skip activity. class); startActivity (intent );}});}}
Invalid activity source code
Package com. example. activitylauchmodepractice; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView; public class extends Activity {private Button btn_jump; private TextView TV _showViewClass; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_second); TV _showViewClass = (TextView) findViewById (R. id. TV _showViewClass); TV _showViewClass.setText ("current Activity:" + "\ n" + this. toString () + "\ n" + "Current TaskId:" + this. getTaskId ());}}
Pay special attention to the following configuration in the listing file:
<activity android:name="com.example.activitylauchmodepractice.ShareActivity" android:launchMode="singleInstance"> <intent-filter> <action android:name="SecondActivity_action"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity>
We need to configure the intent activity action to be used when another application is started.
The source code of Activity2 In the second App is as follows:
Package com. example. singleinstancepractice; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView; public class MainActivity extends Activity {private Button btn_jump; private TextView TV _showTaskId; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); TV _showTaskId = (TextView) findViewById (R. id. TV _showTaskId); TV _showTaskId.setText ("current Activity:" + "\ n" + this. toString () + "\ n" + "Current TaskId:" + this. getTaskId (); btn_jump = (Button) findViewById (R. id. btn_jump); btn_jump.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {Intent intent Intent = new intent (); Intent. setAction ("SecondActivity_action"); startActivity (intent );}});}}
When we open intent activity in the first App and press the back key to return to the original interface, intent activity is an independent entity, in this case, you do not need to create a new sequence activity instance to view the result when opening the sequence activity in the second App. Because the system will automatically search for the result and use it directly if it exists. The schematic diagram is as follows:
Note: It is based on the change process when the first App is running on the mobile phone and the jump button on the second App is clicked to jump to the preview activity.