Android exits an application with multiple activities

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. This article comes from the network. If there are similarities, It is necessary. I wrote this post just to summarize the common things. I still hope the original author is not strange. I really do not want to infringe.

Method 1: Use List to save activity instances, and then kill them one by one.
Code:
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 Entity 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 ();
}

}
Add similar code to 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 );
}

To exit the program, call:

Sysapplication. getinstance (). Exit ();
Sysapplication. getinstance (). Exit ();

In short, you can add reference of each activity to a global linked list in singleton mode. Each time you exit a program and call system. Exit (0), you must first call the finish method of the activity in the linked list.


Appendix: This method can also be referred to link: http://maosidiaoxian.iteye.com/blog/1404725

Method 2: Use Broadcast

2.2 exiting the entire application will inevitably be confusing. The previous attempt was as follows:
Activitymanager manager = (activitymanager) Context. getsystemservice (context. activity_service );
Manager. killbackgroundprocesses (Package );
No

Android. OS. process. killprocess (Android. OS. process. mypid ());
No.

Manager. restartpackage (Package );
Still not good

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, it will be a problem, or it will not work.

We can see that there is a broadcast mechanism, and we can find that it is a good thing to completely solve this problem, without talking nonsense about 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 the broadcast in the current activity
Intentfilter filter = new intentfilter ();
Filter. addaction (attribute. pagename );
Registerreceiver (this. broadcastreceiver, filter); // register
}

/**
* Close
*/
Public void close (){
Intent intent = new intent ();
Intent. setaction (attribute. pagename); // specifies the action
Sendbroadcast (intent); // This function is used to send broadcasts.
Finish ();
}
...
}

The unregisterizer must be added. Otherwise, a bunch of errors may occur and the simplest method is found.

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.