Android app exit

Source: Internet
Author: User

Android app exit

For beginners, the exit of android apps is sometimes a headache. Below are some common exit methods:

In the big direction, the exit idea is nothing more than destroying all the activities of the program. The idea is clear and you can see what tricks you have taken ~

1. Manufacturing throws exceptions and causes the entire program to exit

2. Put all the activities into a list, and then finish all the activities when they need to exit.

3. Broadcasting is used to exit the function.

1. The entire process exits due to manufacturing exception:

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


2. Put all the activities into a list, and then finish all the activities when they need to exit:

Define a set in application


When activity oncreate is used, the corresponding activity is added to the set. When the activity exits, it traverses the previously opened activity and closes it.

For (Activity activity: leCacheApp.OpenActivity){

Activity. finish ();}

Android. OS. Process. killProcess (android. OS. Process. myPid ());

System. exit (0 );


3. Broadcast to exit the function:

When each Activity is created (onCreate), it registers a broadcast receiver for the Activity and sends the broadcast when it exits. The approximate code is as follows:

@ Override


Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
IntentFilter filter = new IntentFilter ();
Filter. addAction ("finish ");
Registerpolicer (mfinishpolicer, 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 ();
}
}

};

When you exit, run the following code to close the full exit program on all interfaces:

GetApplicationContext (). sendBroadcast (new Intent ("finish "));




Related Article

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.