Android activity Startup Mode

Source: Internet
Author: User

Android activity Startup Mode

In addition to some information on the Internet, I summarized the four startup modes of activity.

In actual projects, we should specify an appropriate startup mode for each activity based on specific needs. There are four startup modes: standard, singleTop, singleTask, and singleInstance. You can specify the android: launchMode attribute in AndroidManifest. xml to select the startup mode.

1. standard (this is a standard mode of activity. This mode is used by default when an activity is created)

In standard mode, every time a new activity is started, it will go to the stack in the returned stack and be at the top of the stack. For an activity in standard mode, the system does not care whether the activity already exists in the returned stack. A new instance of the activity will be created each time it is started. Next, let's take a look at the code.

@ Override protected void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

Log. d ("FirstActivity", this. toString ());

RequestWindowFeature (Window. FEATURE_NO_TITLE );

SetContentView (R. layout. first_layout );

Button button1 = (Button) findViewById (R. id. button_1 );


Button1.setOnClickListener (new OnClickListener (){

@ Override public void onClick (View v ){

Intent intent = new Intent (FirstActivity. this, FirstActivity. class );

StartActivity (intent );

}});
}

I personally think that the Code should clearly explain the problem, so I will not explain it again.

2, singleTop

Use singleTop mode. When the Startup Mode of the activity is specified as singleTop, if it is found that the stack top of the returned stack is already the activity during the startup activity, it is considered that it can be used directly and no new active instances will be created.

Modify the Activity Startup Mode in AndroidManifest. xml (android: launchMode = "singleTop ")

Then run the program again to view the LogCat

I personally think that the Code should clearly explain the problem, so I will not explain it again.

3. singleTask

When the active startup mode is specified as singleTask, the system first checks whether the active instance exists in the returned stack each time it starts the activity. If the active instance exists, it is directly used, all the activities above this activity are pushed out of the stack. If no activity is found, a new activity instance is created.

Modify the Activity Startup Mode in AndroidManifest. xml (android: launchMode = "singleTask ")

Then add the onRestart () method to FirstActivity and print the log:

@ Override protected void onRestart (){

Super. onRestart ();

Log. d ("FirstActivity", "onRestart ");

}

Add the onDestroy () method to SecondActivity and print the log:

@ Override protected void onDestroy (){

Super. onDestroy (); Log. d ("SecondActivity", "onDestroy ");

}

Now run the program again. click the button on the FirstActivity interface to go to SecondActivity. Then, click the button on the SecondActivity interface and enter FirstActivity again.

Then run the program again to view the LogCat

4. singleInstance

SingleInstance mode is the most special and complex of the four startup modes. An activity specified as singleInstance mode will enable a new return stack to manage this activity.

I can't explain it either. I don't want to say anything. I just need to go to the code.

First, modify the SecondActivity startup mode (android: launchMode = "singleInstance") in AndroidManifest. xml ")

Code of the onCreate () method in FirstActivity:

@ Override protected void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

Log. d ("FirstActivity", "Task id is" + getTaskId ());

RequestWindowFeature (Window. FEATURE_NO_TITLE );

SetContentView (R. layout. first_layout );

Button button1 = (Button) findViewById (R. id. button_1 );

Button1.setOnClickListener (new OnClickListener (){

@ Override public void onClick (View v ){

Intent intent = new Intent (FirstActivity. this, SecondActivity. class );

StartActivity (intent );

}});}

The id of the current returned stack is printed in the onCreate () method. Then modify the code of the onCreate () method in SecondActivity:

@ Override protected void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

Log. d ("SecondActivity", "Task id is" + getTaskId ());

RequestWindowFeature (Window. FEATURE_NO_TITLE );

SetContentView (R. layout. second_layout );

Button button2 = (Button) findViewById (R. id. button_2 );

Button2.setOnClickListener (new OnClickListener (){

@ Override public void onClick (View v ){

Intent intent = new Intent (SecondActivity. this, ThirdActivity. class );

StartActivity (intent );

}});
}

Similarly, the id of the current returned stack is printed in the onCreate () method, and the Code for clicking the button is modified to start ThirdActivity. Finally, modify the code of the onCreate () method in ThirdActivity:

@ Override protected void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

Log. d ("ThirdActivity", "Task id is" + getTaskId ());

RequestWindowFeature (Window. FEATURE_NO_TITLE );

SetContentView (R. layout. third_layout );

}

The id of the current returned stack is still printed in the onCreate () method. Now run the program again. click the button in the FirstActivity interface to enter SecondActivity, and then click the button in the SecondActivity interface to enter ThirdActivity. Run the program and check the information in LogCat.

This mode is troublesome. I cannot explain it well, but it has limited capabilities. Let's take a look at the code and understand it.

This article is self-fabricated. If there are similarities, it is a coincidence (those principles are found online)

I am not at risk of understanding or understanding mistakes !!!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.