Android development we deal with the most is Activity, Activity has four startup modes, each of which represents a specific use of the situation to understand the startup mode of Activity, you can use the Activity easily.
Activity Standard Startup mode
Standard is the standard mode for using activities. Most of them are the default standard mode, which is also called the default mode code as follows.
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); Tv.settext (" Current activity is :"+ this.toString()+"\n"+" current TaskID:"+getTaskId()); tv.settext (" current activity is :"+ this.toString()+"\n"+" current TaskID:"+getTaskId()); findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this,MainActivity.class)); }}); }Copy the code
The two pictures make it clear to you
The current MainActivity instance is 3364163
The current MainActivity TaskIDIs:
After we start another current Activity,
You can see that the MainActivity instance started again is 436f4f9 and TaskID is 13
From this we can draw the conclusion that:
In Standard mode, all activities are placed on a stack, and each Activity is a separate instance. When we press the Back button, the Activity instance pops up at the top of the stack, and the Activity below the top is displayed at the top of the stack.
Activity SingleTask Startup mode
Take a look at the code MainActivity:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); Tv.settext (" Current activity is :"+ this.toString()+"\n"+" current TaskID:"+getTaskId()); tv.settext (" current activity is :"+ this.toString()+"\n"+" current TaskID:"+getTaskId()); findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this,MainActivity.class)); }}); findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this,BActivity.class)); }}); }Copy the code
BActivity :
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.b); tv = (TextView) findViewById(R.id.tv); Tv.settext (" Current activity is :" + this.toString() + "\n" + "current TaskID:" + getTaskId()); tv.settext (" current activity is :" + this.toString() + "\n" +" current TaskID:" + getTaskId()); findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(BActivity.this, MainActivity.class)); }}); findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(BActivity.this, BActivity.class)); }}); }Copy the code
Steps:
- To initialize MainActivity, start BActivity first
- Start MainActivity in BActivity
At this point, if we hit the back button, it will launch Cheng Hun and return to the phone interface.
Analysis:
At step 1, there are two instances of the activity, MainActivity and BActivity, in the stack. Then we go to Step 2, and there is a change. Instead of creating a new instance of the MainActivity, there is a MainActivity instance in the stack. Instead, use the MainActivity instance that exists at the bottom of the stack.
Violently, MainActivity pops the BActivity on top of it off the stack and takes the top of the stack itself! At this point, there is only one instance of MainActivity in the stack, so pressing the back key will automatically exit the program!
With this picture, cliff got it!
Activity SingleTop Startup mode
Steps:
-
The MainActivity is initialized, and the MainActivity is started without any change, nor is the instance changed
-
Initialize MainActivity, start BActivity, start MainActivity
At this point, the instance of MainActivity changes
Conclusion:
In SingleTop mode, ActivityManager does not create a new instance if the instance is at the top of the stack. Instead, ActivityManager uses the original instance to keep SingleTop unique and calls onNewIntent. An instance will be created again, and there will be two identical instances on the stack.
This figure cliff understand!
Activity SingleInstance Startup mode
This pattern, as shown here
This launch mode is special because it enables a new stack structure, places Acitvity in the new stack structure, and ensures that no other Activity instances enter
Using code
Intent i = new Intent(Intent.ACTION_MAIN)
i.setClassName(activityInfo.applicationInfo.packageName,activityInfo.name)
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(i)Copy the code
Finally, where to configure the boot mode
At this point, the Activity of the four startup modes on the wordy end, understand the four modes, reasonable use, will make the development of twice the result with half the effort. If I can help you, drop by more often. thank you