First, the problem description
In the normal development process can be found that many developers of the program is not to seriously resolve the exit. It's usually either a simple finish (just out of the current activity), or something else, like:
1, the first method: first get the ID of the current process, and then kill the process: Android.os.Process.killProcess (Android.os.Process.myPid ());
2, the second method: terminate the currently running Java Virtual machine, causing the program to terminate: System.exit (0);
3. The third method: Force close all execution associated with the package: Activitymanagermanager = (Activitymanager) getsystemservice (Context.activity_service);
Manager.restartpackage (Getpackagename ());
Use this method to add permissions: <uses-permissionandroid:name= "Android.permission.RESTART_PACKAGES"/>
However, these methods are flawed and do not completely exit the program, such as method one, which does not empty the task stack of the current application's activity. For the third method, it can only kill other applications and not kill itself.
Second, the solution
1, realize the idea: since not all the activity can be closed all at once, the general enterprise's solution is to record all open activity, using a singleton mode to manage the activity, and then exit the program when all open activity is closed off.
2. Code:
⑴ creates a new class app that inherits application, which is created as a global instance of the entire application and needs to be added to the Androidmanifest.xml list.
App class:
[HTML]View Plaincopy
- Package com.example.testexit;
- Import java.util.ArrayList;
- Import java.util.List;
- Import android.app.Activity;
- Import android.app.Application;
- public class App extends application {
- ArrayList<Activity> activities;
- private static APP instance;
- /*
- * Executed when the entire application is created
- */
- @Override
- public void OnCreate () {
- activities = new ArrayList<Activity> ();
- GetInstance ();
- Super.oncreate ();
- }
- public static App getinstance () {
- if (null = = instance) {
- instance = new App ();
- }
- return instance;
- }
- public void Exitapplication () {
- List<Activity> lists = instance.activities;
- for (Activity a:lists) {
- A.finish ();
- }
- }
- }
In other activity, add the current activity in the OnCreate method and remove the activity from the OnDestroy method.
Mainactivity:
[Java]View Plaincopy
- Package com.example.testexit;
- Import Android.os.Bundle;
- Import android.app.Activity;
- Import Android.view.Menu;
- Public class Mainactivity extends Activity {
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.activity_main);
- App app = (APP) getapplication ();
- App.activities.add (this);
- }
- @Override
- Public Boolean oncreateoptionsmenu (Menu menu) {
- Getmenuinflater (). Inflate (R.menu.main, menu);
- return true;
- }
- @Override
- protected void OnDestroy () {
- Super.ondestroy ();
- App app = (APP) getapplication ();
- App.activities.remove (this);
- }
- }
Activity1:
[Java]View Plaincopy
- Package com.example.testexit;
- Import android.app.Activity;
- Import Android.os.Bundle;
- Public class Activity1 extends Activity {
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- App app = (APP) getapplication ();
- App.activities.add (this);
- }
- @Override
- protected void OnDestroy () {
- Super.ondestroy ();
- App app = (APP) getapplication ();
- App.activities.remove (this);
- }
- }
You can also refer to a few other articles about this: http://www.2cto.com/kf/201108/99444.html
http://blog.csdn.net/sgl870927/article/details/6281971
Http://www.cnblogs.com/qingblog/archive/2012/06/08/2541790.html
Solutions for Android Enterprise-class programs to exit completely