Common exit methods for Android apps

Source: Internet
Author: User
Tags throw exception

For beginners, Android app exit is sometimes a headache, the following simple summary of several common exit methods:

Big direction, exit thinking is nothing more than the program all the activity to destroy, the idea is clear, see Make what recruit ~

1. Manufacturing throw exception causes the entire program to exit

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

3. To complete the Exit function via broadcast

1. Manufacturing Throw exception causes the entire program to exit:

See http://blog.csdn.net/liuhe688/article/details/6584143


2. put all the activity into a list and then drop all the activity,finish when you need to exit:

Application define a collection inside


when Activity oncreate, the corresponding activity is added to the collection, when exiting, traverse the previously opened activity and close it.

for (activity activity:Lecacheapp. openactivity) {

Activity.finish ();}

android.os.Process . KillProcess (android.os.Process. Mypid ());

System.exit (0);


3. to complete the Exit function by broadcasting:

When each activity is created (OnCreate), a broadcast receiver is registered with the activity, which is sent when the broadcast exits. 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
& nbsp   public void OnReceive (context context, Intent Intent) {
           if ("Finish". equ ALS (Intent.getaction ())) {
              LOG.E ("#########", "I am" + getlocalclassn Ame ()  + ", now finishing myself ...");
              finish ();
       }
   }

};

Execute the following code on exit to close all interfaces and completely exit the program:

Getapplicationcontext (). Sendbroadcast (new Intent ("Finish"));




Common exit methods for Android apps

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.