Android Learning notes (vii)

Source: Internet
Author: User

Active startup mode

There are four types of activation modes, namely standard, Singletop, Singletask and SingleInstance,

You can select the startup mode in Androidmanifest.xml by assigning the Android:launchmode property to the <activity> tag.

Standard mode

Standard mode is the active default startup mode. For activities that use standard mode, the system does not care if the activity is already present in the return stack.

A new instance of the activity is created each time it is started.

In the Activitytest project, modify the method for OnCreate () in Firstactivity, as shown in the following code:

protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); LOG.D ("Firstactivity", This. toString ()); //Hide title barrequestwindowfeature (Window.feature_no_title); //load the layout in the activity, using the Setcontentview () methodSetcontentview (r.layout.first_layout); Button button1=(Button) Findviewbyid (r.id.button_1); Button1.setonclicklistener (NewOnclicklistener () { Public voidOnClick (View v) {Intent Intent=NewIntent (firstactivity. This, Firstactivity.class);                                            StartActivity (Intent);    }        }); }

The above code, on the basis of firstactivity, starts firstactivity. Run the program, and then click on the Firstactivity interface two times in a row, you can see the Logcat printed information as shown in:

From the printed information, you can see that every click of a button creates a new Firstactivity instance. At this point, there are three instances of firstactivity in the stack, which need to be pressed three times for the back key to exit the program.

Singletop mode

When the active startup mode is Singletop mode, when the activity is started, if it is found that the stack top of the return stack is already the activity,

You can use it directly, and you will not create an instance of the new activity.

To modify the startup mode of firstactivity in Androidmanifest.xml, the code is as follows:

<ActivityAndroid:name=". Firstactivity "Android:label= "This is firstactivity"Android:launchmode= "Singletop" >                        <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN"/>                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>

Rerun the program to view the print information for Logcat as shown in:

In the printed information, there is only one instance of Firstactivity, at which time no new printing information will appear, regardless of the number of buttons you press.

Because the current firstactivity is already at the top of the stack at the back of the stack, the top activity is used directly whenever a firstactivity is started.

However, when firstactivity is not at the top of the stack, the firstactivity is started and a new instance is created.

Singletask mode

In singletop mode, if firstactivity is not at the top of the stack, it is necessary to create an instance of Firstactivity if you want to start firstactivity.

If you want to have only one instance of an activity in the context of the entire program, use the Singletask pattern.

When the active startup mode is specified as Singletask, each time the activity is started, the system first checks the return stack for the existence of an instance of the activity.

If found, it is used directly, and all activities above the activity are stacked, and if not found, a new activity instance is created.

To modify the startup mode of firstactivity in Androidmanifest.xml, the code is as follows:

<ActivityAndroid:name=". Firstactivity "Android:label= "This is firstactivity"Android:launchmode= "Singletask" >                        <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN"/>                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>

Rerun the program to view the Logcat print log information as shown in:

As you can see from the printing information, when you start firstactivity in secondactivity, you find that an instance of firstactivity already exists in the return stack.

And under the secondactivity, so secondactivity will stack from the back of the stack, and firstactivity again become the top of the stack activity,

So Firstactivity's OnStart () method and Secondactivity's OnDestroy () method are executed.

SingleInstance mode

Activities specified as SingleInstance mode will start a new return stack to manage this activity. Each application will have its own return stack,

A new instance must be created when the same activity is stacked on a different return stack.

Using the SingleInstance mode solves this problem, and in this mode there is a separate return stack to manage the activity,

Regardless of which app accesses this activity, the same return stack is shared.

To modify the startup mode of secondactivity in the Androidmanifest.xml file, the code is as follows:

<ActivityAndroid:name=". Secondactivity "Android:launchmode= "SingleInstance" >            <Intent-filter>                <ActionAndroid:name= "Com.example.activitytest.ACTION_START"/>                <categoryAndroid:name= "Android.intent.category.DEFAULT" />                <categoryAndroid:name= "Com.example.activitytest.MY_CATEGORY" />            </Intent-filter>        </Activity>

In the code, set the startup mode of Secondactivity to SingleInstance, and then modify the code for the OnCreate () method in Firstactivity, as follows:

protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); LOG.D ("Firstactivity", "Task ID is" +GetTaskID ()); //Hide title barrequestwindowfeature (Window.feature_no_title); //load the layout in the activity, using the Setcontentview () methodSetcontentview (r.layout.first_layout); Button button1=(Button) Findviewbyid (r.id.button_1); Button1.setonclicklistener (NewOnclicklistener () { Public voidOnClick (View v) {Intent Intent=NewIntent (firstactivity. This, Secondactivity.class);                                            StartActivity (Intent);    }        }); }

Print the ID of the current return stack in the OnCreate () method, and then modify the code for the OnCreate () method in Secondactivity, as follows:

protected voidonCreate (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 (NewOnclicklistener () {@Override Public voidOnClick (View v) {Intent Intent=NewIntent (secondactivity. This, Thirdactivity.class);                            StartActivity (Intent);            }        }); }

Also print the ID of the current return stack in the OnCreate () method, and then modify the code of the button click event to start the thirdactivity.

Finally, the code for the OnCreate () method in Thirdactivity is modified as follows:

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

Prints the ID of the current return stack in the OnCreate () method. Re-run the program, click the button on the Firstactivity interface to enter Secondactivity,

Then click on the button in the Secondactivity interface to enter thirdactivity. To view the printed information for Logcat, get the following:

As you can see from the print message, the Secondactivity task ID is not the same as the firstactivity and thirdactivity.

This means that secondactivity is placed in a separate return stack. In the Thirdactivity interface, pressing the Back button will return directly to the Firstactivity interface,

Press the back key again to return to secondactivity and press the back key to exit the program.

The reason is that thirdactivity and firstactivity are in the same return stack, and when you press the back key from the Thirdactivity interface,

Thirdactivity will stack up from the back stack, then the firstactivity will be displayed on the interface as the top activity of the stack.

Then press the back key again in the Firstactivity interface, when the current return stack is empty, so it shows another stack top activity of the return stack, namely secondactivity.

Android Learning notes (vii)

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.