Two classic ways to quit multiple activity in Android

Source: Internet
Author: User

Here are two methods: one to remember each activity and then one to kill it, and the other to use the radio.

method One, use list to save the activity instance, and then kill each

On the code:

import java.util.linkedlist;import java.util.list;import android.app.activity;import android.app.AlertDialog; Import Android.app.application;import android.content.dialoginterface;import android.content.Intent; Public classSysapplication extends Application {PrivateList<activity> mlist =NewLinkedlist<activity>();Private StaticSysapplication instance;Privatesysapplication () {} PublicSynchronizedStaticsysapplication getinstance () {if(NULL==instance) {Instance=Newsysapplication (); }         returninstance; }//Add Activity Public voidaddactivity (activity activity) {Mlist.add (activity);}  Public voidexit () {Try {            for(Activity activity:mlist) {if(Activity! =NULL) Activity.finish (); }             } Catch(Exception e) {e.printstacktrace (); } finally{system.exit (0); }} @Override Public voidonlowmemory () {super.onlowmemory (); System.GC ();}} Add similar code to each activity's OnCreate method: [Java] Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Sysapplication.getinstance (). Addactivity ( This); }        Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Sysapplication.getinstance (). Addactivity ( This); When you need to exit the program, call: Sysapplication.getinstance (). exit (); Sysapplication.getinstance (). exit (); In short, the reference to each activity is added to a global list by using a singleton mode, and each exit program calls System.exit (0), first call the activity's finish method in the linked list: This method can also refer to the link: http://maosidiaoxian.iteye.com/blog/1404725
method Two, use broadcast

//2.2 Exit the entire application is inevitably confusing, once tried the way:Activitymanager Manager =(Activitymanager) Context.getsystemservice (Context.activity_service); Manager.killbackgroundprocesses (package);//notandroid.os.Process.killProcess (Android.os.Process.myPid ()); //or not .Manager.restartpackage (package);//or not?Intent myintent=NewIntent (Intent.action_main);   Myintent.addcategory (Intent.category_home);   StartActivity (myintent); Finish ();//This is just going to go back to the desktop, if you open multiple activity close reopen also have a problem, or still not//see someone said that the broadcasting mechanism, found to be a good thing, can completely solve this problem, nonsense do not say look at the code: Public Abstract classEnteractivity extends Baseactivity { ...//write an internal class for the broadcast, and when the action is received, end the activity  PrivateBroadcastreceiver Broadcastreceiver =NewBroadcastreceiver () {@Override Public voidOnReceive (Context context, Intent Intent) {unregisterreceiver ( This);//This sentence must be written to not error, not write although can close, will report a pile of mistakes( Activity) (context). Finish ();   }       }; @Override Public voidOnresume () {super.onresume ();//registering a broadcast in the current activityIntentfilter filter =NewIntentfilter ();     Filter.addaction (Attribute.pagename); Registerreceiver ( This. broadcastreceiver, filter);//Register} Public voidClose () {Intent Intent=NewIntent (); Intent.setaction (Attribute.pagename); //Description ActionSendbroadcast (Intent);//This function is used to send a broadcastfinish (); }      ... } Someone else wrote a missing place (Unregisterreceiver) This must be added, or there will be a lot of mistakes, find the simplest way. 

Two classic ways to quit multiple activity in Android

Two classic ways to quit multiple activity in Android

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.