Android will call android when exiting the application. OS. process. killProcess (android. OS. process. myPid () or System. exit (0), which is only effective for the first Activity (that is, the Activity of the entry. If there are three activities A, B, and C, and you want to exit the Activity in B or C, calling the above method will usually destroy the current Activity and return the previous Activity. Of course, you can also return the previous Activity one by one until you jump to the Activity at the entry, and finally exit the application. However, this is troublesome and the return experience is unfriendly.
The most popular method on the Internet is to define the stack, write an ExitApplication class, manage the Activity in singleton mode, and call ExitApplication in each onCreate () method of the Activity. getInstance (). addActivity (this) method, which calls ExitApplication when exiting. getInstance (). exit () method to completely exit the application.
ExitApplication class
The Code is as follows:
Copy codeThe Code is as follows: import java. util. Collections list;
Import java. util. List;
Import android. app. Activity;
Import android. app. Application;
Public class ExitApplication extends Application {
Private List activityList = new shortlist ();
Private static ExitApplication instance;
Private ExitApplication ()
{
}
// Obtain a unique ExitApplication instance in singleton Mode
Public static ExitApplication getInstance ()
{
If (null = instance)
{
Instance = new ExitApplication ();
}
Return instance;
}
// Add the Activity to the container
Public void addActivity (Activity activity)
{
ActivityList. add (activity );
}
// Traverse all activities and finish
Public void exit ()
{
For (Activity activity: activityList)
{
Activity. finish ();
}
System. exit (0 );
}
}
The following three types of IndexActivity, BActivity, and CActivity are simple examples of the jump sequence of IndexActivity-> BActivity-> CActivity. Call the ExitApplication. getInstance (). addActivity (Activity) method in the onCreate () method of each activity class. When you exit an application on any Activity interface, you can completely exit the application in any Activity by calling the ExitApplication. getInstance (). exit () method.
Source code of the IndexActivity class:
Copy codeThe Code is as follows: import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Public class IndexActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Button next = (Button) findViewById (R. id. next_to_ B );
Next. setOnClickListener (nextClick );
Button exit = (Button) findViewById (R. id. exit_main );
Exit. setOnClickListener (exitClick );
ExitApplication. getInstance (). addActivity (this );
}
OnClickListener nextClick = new OnClickListener (){
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Intent intent = new Intent (IndexActivity. this, BActivity. class );
StartActivity (intent );
}
};
OnClickListener exitClick = new OnClickListener (){
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
ExitApplication. getInstance (). exit ();
}
};
}
BActivity source code:
Copy codeThe Code is as follows: import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Public class BActivity extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
SetContentView (R. layout. B );
Button next_to_c = (Button) findViewById (R. id. next_to_c );
Next_to_c.setOnClickListener (next_to_cClick );
Button exit_ B = (Button) findViewById (R. id. exit_ B );
Exit_ B .setOnClickListener (exitClick );
ExitApplication. getInstance (). addActivity (this );
}
OnClickListener next_to_cClick = new OnClickListener (){
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Intent intent = new Intent (BActivity. this, CActivity. class );
StartActivity (intent );
}
};
OnClickListener exitClick = new OnClickListener (){
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
ExitApplication. getInstance (). exit ();
}
};
}
CActivity source code:
Copy codeThe Code is as follows: import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Public class CActivity extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
SetContentView (R. layout. c );
Button exit_c = (Button) findViewById (R. id. exit_c );
Exit_c.setOnClickListener (exitClick );
ExitApplication. getInstance (). addActivity (this );
}
OnClickListener exitClick = new OnClickListener (){
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
ExitApplication. getInstance (). exit ();
// If you only call one of the following methods, the application will not be completely exited.
// Android. OS. Process. killProcess (android. OS. Process. myPid ());
// System. exit (0 );
}
};
}
Download source code: ExitActivity.zip