Activity four startup Modes 1

Source: Internet
Author: User

First, the introduction of the starting mode

The startup mode is simply that the activity starts with the policy, the Android:launchmode property of the label in the Androidmanifest.xml is set;

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 stack

Each application has a task stack, which is a stack of functions similar to that of a function call, and the order represents the order in which the activity appears; For example, Activity1-->activity2-->activity3, the task stack is:

650) this.width=650; "src=" Http://www.linuxidc.com/upload/2012_07/120721093646151.gif "style=" border:0px; "/>

Second, the starting mode

(1) Standard: Every time the activity is activated (startactivity), the activity instance is created and put into the task stack;

650) this.width=650; "src=" Http://www.linuxidc.com/upload/2012_07/120721093646152.gif "width=" 580 "vspace=" 5 "style = "border:0px;"/>

(2) Singletop: If an activity itself activates itself, that is, the task stack top is the activity, you do not need to create, the rest of the case to create activity instances;

650) this.width=650; "src=" Http://www.linuxidc.com/upload/2012_07/120721093646153.gif "width=" 580 "vspace=" 5 "style = "border:0px;"/>

(3) Singletask: If the activity to be activated in the task stack exists in the instance, you do not need to create, just need to put this activity into the top of the stack, and the activity of the activity above the instance of pop;

650) this.width=650; "src=" Http://www.linuxidc.com/upload/2012_07/120721093646154.gif "width=" 580 "vspace=" 5 "style = "border:0px;"/>

(4) SingleInstance: If Mainactivity instance is created in the task stack of application 1, if Application 2 also activates mainactivity, then no need to create, two apps to share the activity instance;

650) this.width=650; "src=" Http://www.linuxidc.com/upload/2012_07/120721093646155.gif "style=" border:0px; "/>

Application of Singtask:

can be used to exit the entire application.

Set the main activity to Singtask mode, then transfer the activity to the main activity in order to exit, then rewrite the onnewintent function of the main activity and add a finish to the function.

Report:

To exit a single activity method:

Call Finish

Kill the Process: KillProcess (Process.mid)

To terminate a running virtual machine: System.exit ()

Exit the entire application:

Manufacturing throw 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 exit.

To complete the Exit function by broadcasting

The completion of the Exit function via broadcast is accomplished by registering a broadcast receiver with the activity at each activity creation (OnCreate) and sending the broadcast when exiting. The approximate code is as follows:

650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>

650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>

@Overrideprotected  void oncreate (bundle savedinstancestate)  {        super.oncreate (savedinstancestate);        intentfilter  filter = new intentfilter ();        filter.addaction (" Finish ");        registerreceiver (Mfinishreceiver, filter);       &NBSP, ...} Private broadcastreceiver mfinishreceiver = new broadcastreceiver ()  {      @Override     public void onreceive (context context,  intent intent)  {           if ("Finish". Equals (Intent.getaction ()))  {              LOG.E ("#########",   "i am "  + getlocalclassname ()     &Nbsp;                +  ", Now finishing myself ... ");               finish ();       }    }};


Activity four startup Modes 1

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.