(Android first line of code) active startup mode

Source: Internet
Author: User

Active startup mode

There are four starting modes, standard, Singletop, Singletask and singleinstance, which can be specified in the Androidmanifest.xml by assigning the <activity> tag The Android:launchmode property to select the startup mode. Let's study one by one.

1. Standardmode : (for activities that use standard mode, the system does not care whether the activity is already present in the return stack, and each startup creates a new instance of the activity.) )

is the active default startup mode

Modify the code for the OnCreate () method in Firstactivity as follows:

@Override

protected void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

//From the printed message we can see that every click of the button will create a new firstactivity instance we need to press three times to exit the program.

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) {

//We, start firstactivity on the basis of firstactivity.

Intent Intent = new Intent (firstactivity.this, Firstactivity.class);

StartActivity (Intent);

}

});

}

2.singletop mode (when initiating an activity, if it is found that the stack at the top of the stack is already the activity, it is considered straightforward to use and no new activity instances are created.) )

Modify the startup mode of firstactivity in Androidmanifest.xml,

<activity

Android:name= ". Firstactivity "

//Modify the startup mode of firstactivity in Androidmanifest.xml,

android:launchmode= "Singletop"

Android:label= "This is firstactivity" >

<intent-filter>

<action android:name= "Android.intent.action.MAIN"/>

<category android:name= "Android.intent.category.LAUNCHER"/>

</intent-filter>

</activity>

Then rerun the program and view Logcat to see that an instance of firstactivity has been created. Press the back key once to exit the program. However, when the firstactivity is not at the top of the stack, then the firstactivity is started and a new instance is created.

3.singletask mode (when the active startup mode is specified as Singletask, each time the activity is started, the system first checks the return stack for an instance of the activity, and if the discovery already exists, uses the instance directly and All activities on top of each activity, and if not found, a new activity instance is created. )

    • Modifying the startup mode of firstactivity in Androidmanifest.xml

<activity

Android:name= ". Firstactivity "

//Modify the startup mode of firstactivity in Androidmanifest.xml

android:launchmode= "Singletask"

Android:label= "This is firstactivity" >

<intent-filter>

<action android:name= "Android.intent.action.MAIN"/>

<category android:name= "Android.intent.category.LAUNCHER"/>

</intent-filter>

</activity>

    • Then add the Onrestart () method to the Firstactivity and print the log:

@Override

protected void Onrestart () {

Super.onrestart ();

LOG.D ("Firstactivity", "Onrestart");

}

    • Finally, add the OnDestroy () method to the Secondactivity and print the log:

@Override protected void OnDestroy () {

Super.ondestroy ();

LOG.D ("Secondactivity", "OnDestroy");

}

When starting firstactivity in Secondactivity, it is found that an instance of firstactivity already exists in the return stack and is below secondactivity, so secondactivity will stack from the back stack. The firstactivity is again a top-of-stack activity, so the firstactivity Onrestart () method and the Secondactivity OnDestroy () method are executed.

4.singleinstance mode (the activity specified as SingleInstance mode will enable a new return stack to manage this activity)

usage Scenario: Suppose that one of our programs has an activity that allows other programs to call, if we want to implement other programs and our program can share an instance of this activity . Using the previous three startup modes is definitely not going to work because each application will have its own return stack , and a new instance must be created when the same activity is stacked on a different return stack . The problem can be solved by using the singleinstance mode, in which a separate return stack is used to manage the activity , regardless of which application accesses the activity, and the same return stack is shared.

    • Modify the Androidmanifest. The startup mode of secondactivity in XML:

<activity

Android:name= ". Secondactivity "

// Modify the startup mode of secondactivity in Androidmanifest. xml:

android:launchmode= "SingleInstance" >

<intent-filter>

<action android:name= "Com.example.activitytest.ACTION_START"/>

<category android:name= "Android.intent.category.DEFAULT"/>

<category android:name= "Com.example.activitytest.MY_CATEGORY"/>

</intent-filter>

</activity>

    • Modifies the code of the OnCreate () method in Firstactivity, and prints the ID of the current return stack in the OnCreate () method.

log.d ("firstactivity", "Task ID is" + gettaskid ());

    • Modifies the code of the OnCreate () method in secondactivity, printing the ID of the current return stack.

log.d ("secondactivity", "Task ID is" + gettaskid ());

    • The last modification of the OnCreate () method in thirdactivity still prints the ID of the current return stack in the OnCreate () method.

log.d ("thirdactivity", "Task ID is" + gettaskid ());

Printing results:

As you can see, the secondactivity Task ID differs from firstactivity and thirdactivity, which means that secondactivity is actually stored in a separate return stack, and that the stack has only Secondactivity this one activity.

(Android first line code) active startup mode

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.