Android: singleTop in Activity Startup Mode
Let's take a look at the description of singleTop Startup Mode:
There can be multiple instances, but multiple instances of the Activity cannot be superimposed. That is, if an instance of this Activity starts at the top of the stack, the OnNewIntent method will be called instead of creating a new instance. If the instance is not at the top of the stack, a new instance will be created.
Next, let's take a closer look at it through an instance.
This is the effect on mobile phones.
First, there are two buttons in the xml layout file.
Create two classes that inherit the Activity, MainActivity and OtherActivity. The code is basically the same and both use the above xml layout file.
MainActivity. class
Package com. example. singletop; import android. OS. bundle; import android. app. activity; import android. content. intent; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. button; public class MainActivity extends Activity implements OnClickListener {private Button btn_open1, btn_open2; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // set the title to Activity1setTitle ("I am Activity1"); btn_open1 = (Button) findViewById (R. id. button1); btn_open2 = (Button) findViewById (R. id. button2); btn_open1.setOnClickListener (this); btn_open2.setOnClickListener (this) ;}@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubswitch (v. getId () {case R. id. button1: startActivity (new Intent (MainActivity. this, MainActivity. class); break; case R. id. button2: startActivity (new Intent (MainActivity. this, OtherActivity. class); break ;}}}
OtherActivity. class
Package com. example. singletop; import android. OS. bundle; import android. app. activity; import android. content. intent; import android. util. log; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. button; public class OtherActivity extends Activity implements OnClickListener {private Button btn_open1, btn_open2; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // set the title to Activity2setTitle ("I am Activity2"); btn_open1 = (Button) findViewById (R. id. button1); btn_open2 = (Button) findViewById (R. id. button2); btn_open1.setOnClickListener (this); btn_open2.setOnClickListener (this) ;}@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubswitch (v. getId () {case R. id. button1: startActivity (new Intent (OtherActivity. this, MainActivity. class); break; case R. id. button2: startActivity (new Intent (OtherActivity. this, OtherActivity. class); break ;}}// print the log for calling this method @ Overrideprotected void onNewIntent (Intent intent) {// TODO Auto-generated method stubsuper. onNewIntent (intent); Log. d ("OtherActivity", "OnNewIntent ");}}
As for the differences between MainActivity and OtherActivity:
MainActivity is in standard startup mode.
OtherActivity is singleTop Startup Mode
In AndroidManifest. xml, MainActivity is the standard startup mode by default.
Configure OtherActivity.
<activity android:name="com.example.singletop.OtherActivity" android:launchmode="singleTop"></activity>
Let's take a few examples.
Activity1-> Activity2-> Activity1-> Activity2
When we finish these operations on the stack
Then, we can see that the onNewIntent method is not called because Activity2 is not at the top of the stack when Activity2 in singleTop startup mode is created. You can only return to the main interface after four responses.
2. Activity1->Activity2->Activity2->Activity2
What is the situation in the stack at this time?
That's right. Why? When we create the third Activity2, because Activity2 is the singleTop startup mode, we find that Activity2 created in the second is the top of the stack.The OnNewIntent method is called instead of creating a new instance. The same is true for creating the fourth Activity2. At this time, we only need to press the return key twice to return to the main interface. Next let's take a look at the log information
Do you understand this startup mode when you see your friends here?
Let's get a better impression ~
Description of singleTop Startup Mode:
There can be multiple instances, but multiple instances of the Activity cannot be superimposed. That is, if an instance of this Activity starts at the top of the stack, the OnNewIntent method will be called instead of creating a new instance. If the instance is not at the top of the stack, a new instance will be created.