Http://www.ilingxi.com/blog/duanhongchang? P = 72
We all know that the finish method of the activity can only exit the current activity. But what if we want to exit all created activities with one click? We designed a class to manage the activity of the current activity through the singleton mode, called myactivitymanager. The Code is as follows:
Import java. util. Collections list;
Import java. util. List;
Import Android. App. activity;
Import Android. App. Application;
/**
* Activity management class
* 1. Activity (): Save Activity
* 2. Exit (): Close all stored activities.
* @ Author Duan Hongchang
*
*/
Public class myactivitymanager extends application {
/**
* Activity list
*/
Private list <activity> activitylist = new shortlist <activity> ();
/**
* Globally unique instance
*/
Private Static myactivitymanager instance;
/**
* This class adopts the singleton mode and cannot be instantiated.
*/
Private myactivitymanager ()
{
}
/**
* Getting class instance objects
* @ Return myactivitymanager
*/
Public static myactivitymanager getinstance (){
If (null = instance ){
Instance = new myactivitymanager ();
}
Return instance;
}
/**
* Save the activity to the existing list.
* @ Param Activity
*/
Public void addactivity (activity ){
Activitylist. Add (activity );
}
/**
* Close the saved activity.
*/
Public void exit (){
If (activitylist! = NULL)
{
Activity activity;
For (INT I = 0; I <activitylist. Size (); I ++ ){
Activity = activitylist. Get (I );
If (activity! = NULL)
{
If (! Activity. isfinishing ())
{
Activity. Finish ();
}
Activity = NULL;
}
Activitylist. Remove (I );
I -;
}
}
}
}
Call the addactivity method of myactivitymanager in the oncreate method of each activity, and then call the exit method in myactivitymanager where one-click exit is required.