Android enterprise-level program exit solution

Source: Internet
Author: User

I. Problem Description

During normal development, we can find that many developers did not seriously solve the exit of the program. Generally, it is either a simple finish (just exit the current activity) or another method, such:


1. Method 1: first obtain the id of the current Process, and then kill the Process: android. OS. Process. killProcess (android. OS. Process. myPid ());


2. Method 2: Terminate the currently running Java virtual machine, resulting in program termination: System. exit (0 );

3. Method 3: Force disable all executions associated with the package: ActivityManagermanager = (ActivityManager) getSystemService (Context. ACTIVITY_SERVICE );
Manager. restartPackage (getPackageName ());

To use this method, you must add the following permissions: <uses-permissionandroid: name = "android. permission. RESTART_PACKAGES"/>

However, these methods are flawed and cannot completely exit the program. For example, method 1 does not empty the task stack of the activity of the current application. For the third method, it can only kill other applications but not itself.

 


Ii. Solutions

1. Implementation idea: Since you cannot close all the activities at a time, the general solution for an enterprise is to record all opened activities and manage the activities in singleton mode, close all opened activities when exiting the program.

2. Code:

(1) create a new class App that inherits the Application. It is used to create a global instance of the entire Application and needs to be added to the AndroidManifest. xml list.

App Type:


[Html]
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;
 
/*
* Run the command 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 ();
}
}
}

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;

/*
* Run the command 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 activities, add the current Activity to the onCreate method, and then remove the Activity from the onDestroy method.

MainActivity:


[Java]
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 );
 
}
 
}

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]
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 );
}
 

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.