Android Starter Activity four startup modes (standard, Singletop, Singletask, singleinstance) _android

Source: Internet
Author: User

When the application runs, it opens a thread that runs a task stack, which is placed in the task stack when the activity instance is created. The activity startup mode is set in the Androidmanifest.xml file by configuring the properties of the activity android:launchmode= "" setting.

Introduction of Starting Mode

The startup mode is simply the strategy of activity initiation, and the Android:launchmode attribute setting of tags in androidmanifest.xml;

The starting mode has 4 kinds, namely standard, Singletop, Singletask, singleinstance;

Before explaining the starting mode, it is necessary to explain the concept of "task stack" first;

  Task stacks

Each application has a task stack, which is used to store the activity, functions similar to the stack of function calls, the order represents the order of activity, such as Activity1-->activity2-->activity3, the task stack is:

Second, start mode

(1) Standard mode (default): Every time the activity is activated (startactivity), the active instance is created and placed on the task stack;

The activity that we create directly in this pattern is a feature of this model: as long as you create an active instance, once you activate it, you add the newly created instance to the task stack. Exiting an activity destroys the instance in the task stack.

(2) Singletop mode: If an activity activates itself, that is, at the top of the task stack, it does not need to be created, and the rest of the activity instance is created;


(3) Singletask mode: If the activity that is to be activated is present in the task stack, it does not need to be created, just put it on the top of the stack and pop all the active instances above it;

If the activity instance of the pattern exists in the task stack, remove all instances of the instance above the stack, and invoke the Newinstance () method of the instance to reuse it so that the instance is on top of the stack. Otherwise, a new activity instance is recreated.

(4) SingleInstance mode: If the application of 1 of the task stack to create a mainactivity instance, if the application of 2 also to activate mainactivity, you do not need to create, two applications to share the activity instance;

When the pattern activity instance is created in the task stack, as long as the instance is still in the task stack, that is, as long as it is activated by that type, it will be reused by invoking the Newinstance () method of the instance. The same activity instance is used at this time, and it will be at the top of the stack on the task stack. This pattern is typically used to load slow, performance-consuming activity that does not need to be recreated every time.

Application of Singtask:

can be used to exit the entire application.

The main activity is set to Singtask mode, then the activity to be exited is transferred to the main activity, then the Onnewintent function of the main activity is overridden and a finish is added to the function.

Report:

To exit a single activity method:

Call Finish

Kill the Process: KillProcess (Process.mid)

Terminating a running virtual machine: System.exit ()

Exit the entire application:

Manufacturing throws exception causes the entire program to exit

Put all the activity into a list and then drop all the activity,finish when you need to quit.

To complete the Exit function by broadcasting

The Exit function is accomplished by broadcasting, which is done by registering a broadcast receiver for the activity at the time of each activity creation (OnCreate) and sending the broadcast when exiting. The approximate code is as follows:

@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Intentfilter filter = new Intentfilter ();
Filter.addaction ("Finish");
Registerreceiver (mfinishreceiver, filter);
...
}
Private Broadcastreceiver Mfinishreceiver = new Broadcastreceiver () {
@Override public
void OnReceive (context Context, Intent Intent) {
if ("Finish". Equals (Intent.getaction ())) {
log.e ("#########", "I am" + Getlocalclassname ()
+ ", now finishing myself ...");
Finish ();
}

Write the above code in a base class so that you can call close at the place where you want to exit, send a broadcast, and each activity receives the broadcast, and then it will finish off.

The above is a small set for everyone to share the Android start activity four kinds of starting mode (standard, Singletop, Singletask, singleinstance), I hope you like.

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.