Two typical methods for exiting multiple activities in Android

Source: Internet
Author: User

Here we will introduce two methods: one is to remember each activity and then kill them one by one; the other is to use broadcast.

Method 1: Use List to save activity instances, and then kill them one by one.

UpperCode:

 Import  Java. util. Collections list;  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   Class Sysapplication Extends  Application {  Private List <activity> MList = New History List <activity> ();  Private   Static  Sysapplication instance;  Private  Sysapplication (){} Public   Synchronized   Static  Sysapplication getinstance (){  If ( Null = Instance) {instance = New  Sysapplication ();}  Return  Instance ;}  //  Add Activity  Public   Void Addactivity (activity) {MList. Add (activity );}  Public   Void  Exit (){  Try  {  For  (Activity: MList ){  If (Activity! = Null  ) Activity. Finish ();}}  Catch  (Exception e) {e. printstacktrace ();} Finally  {System. Exit ( 0 ) ;}}@ Override  Public   Void  Onlowmemory (){  Super  . Onlowmemory (); system. GC () ;}} added similar code in the oncreate method of each activity: [Java]  Public   Void  Oncreate (bundle savedinstancestate ){  Super  . Oncreate (savedinstancestate); sysapplication. getinstance (). addactivity ( This  );}  Public   Void  Oncreate (bundle savedinstancestate ){  Super  . Oncreate (savedinstancestate); sysapplication. getinstance (). addactivity (  This  );} Exit as neededProgramCall: sysapplication. getinstance (). exit (); sysapplication. getinstance (). exit (); in short, the reference of each activity is added to a global linked list in singleton mode, and the system is called every time the program exits. exit ( 0 ), First call the finish method of the activity in the linked list. For more information about this method, see http:  // Maosidiaoxian.iteye.com/blog/1404725 

 

Method 2: Use Broadcast
// 2 . 2 exiting the entire application will inevitably be confusing. Once upon a while, we tried the following method: activitymanager = (Activitymanager) Context. getsystemservice (context. activity_service); manager. killbackgroundprocesses (  Package  ); // No Android. OS. process. killprocess (Android. OS. process. mypid (); // no manager. restartpackage (  Package  ); // Still cannot use intent myintent = New Intent (intent. action_main); myintent. addcategory (intent. category_home); startactivity (myintent); finish (); // This is only returned to the desktop. If you open multiple activities and close them again, the problem may occur, still not working. // when someone says that the broadcast mechanism is good, the problem can be completely solved. If you don't talk nonsense, read the code:  Public   Abstract   Class Enteractivity Extends  Baseactivity {...  //  Write an internal broadcast class. When an action is received, the activity ends.  Private Broadcastreceiver = New  Broadcastreceiver () {@ override Public   Void  Onreceive (context, intent) {unregisterreceiver (  This ); //  This statement must be written to avoid errors. If it is not written, a bunch of errors will be reported even if it can be closed.  (Activity) Context). Finish () ;};@ override  Public   Void  Onresume (){  Super  . Onresume ();  //  Register broadcast in current activity Intentfilter filter = New  Intentfilter (); filter. addaction (attribute. pagename); registerreceiver (  This . Broadcastreceiver, filter ); //  Register  }  Public   Void  Close () {intent = New  Intent (); intent. setaction (attribute. pagename );  //  Action Description Sendbroadcast (intent );//  This function is used to send broadcasts.  Finish () ;}...} someone else wrote something missing (unregisterreceiver). This must be added. Otherwise, a bunch of errors will occur and the simplest method is found. 

This article is collected on the Internet, but I don't know the source address. Thank you for providing it. Sorry, I am sorry. Let's learn it together!

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.