Startup mode for Android activity

Source: Internet
Author: User

Own understanding plus some information on the Web summarizes four startup modes for activity

In the actual project we should specify the appropriate startup mode for each activity according to the specific requirements. 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.

1,standard (this is a standard mode of activity, which is the default mode when creating an activity)

In standard mode, whenever a new activity is started, it is placed in the stack on the back of the stack and at the top of the stack. For activities that use standard mode, the system does not care if the activity is already present in the return stack, and a new instance of the activity is created each time it is started. Next, let's look at a piece of 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 be quite clear about the problem, so I won't say it anymore.

2,singletop

Use Singletop mode. When the active startup mode is specified as Singletop, if it is found that the stack at the top of the return stack is already active at the start of the activity, it is considered that it can be used directly and no new activity instances will be created.

The activity's startup mode (android:launchmode= "Singletop") is now modified in Androidmanifest.xml

Then rerun the program to view LogCat

I personally think that the code should be quite clear about the problem, so I won't say it anymore.

3, Singletask

When the active startup mode is specified as Singletask, each time the activity is started, the system first checks to see if there is an instance of the activity in the return stack, uses the instance directly if the discovery already exists, and puts all the activities on top of the activity out of the stack, creating a new instance of the activity if no discovery is found.

The activity's startup mode (android:launchmode= "Singletask") is now modified in Androidmanifest.xml

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");

}

Now rerun the program, click the button in the Firstactivity interface to enter the Secondactivity, and then click on the Secondactivity interface button, will re-enter to Firstactivity.

Then rerun the program to view LogCat

4,singleinstance

The singleinstance mode should be the most special and complex of the four startup modes, and activities designated as SingleInstance mode will enable a new return stack to manage this activity.

I am not good at explaining, nothing to say, on the code.

Modify the Androidmanifest first. Secondactivity startup mode in XML (android:launchmode= "SingleInstance")

Code for 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 return stack is printed in the OnCreate () method. Then modify the code for 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);

}});
}

It also prints the ID of the current return stack in the OnCreate () method, and then modifies the code of the button click event to start the thirdactivity. Finally, modify the code for 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 return stack is still printed in the OnCreate () method. Now rerun the program, click on the Firstactivity interface button to enter the Secondactivity, and then click on the Secondactivity interface button to enter the Thirdactivity. Run the program to see the information in the Logcat.

This model is more troublesome, I also explain not good, ability Limited, we look at the code to understand it.

This article is a random fiction, if the similarities are purely coincidental (those principles are online search)

I do not accept any can not understand the risk of writing wrong!!!


Startup mode for Android activity

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.