When doing the project will encounter the application can not completely quit the problem, online search, the more popular is to add each activity to a stack, exit the time to empty the stack. Here is a complete class, containing the corresponding method, need to use the time to call directly.
Instantiates an object within each activity, and then pushes each activity into the stack through the Addactivity () method. Call the exit () method when you need to exit
When you need to use, you can directly copy the following code, modify the class name to
For example, I need to put the activity into the stack only myapplication.getinstance (). Addactivity (activity)
Exit directly call Myapplication.getinstance (). exit () or other method
public class MyApplication extends application {public list<fragment> mgetgoldcoinsfragments;public long Mlogintime;private static MyApplication instance;private static final String tag = "MyApplication";p rivate static Context appcontext;//determines whether the home Activity is a stop state private final list<activity> activitylist = new linkedlist<activity> ();p rivate long mexittime;public static MyApplication getinstance () {if (null = = Instance) {instance = new MyApplication () ;} return instance;} /** * Add activity to Container * * @param activity */public void addactivity (activity activity) {Activitylist.add (activity);} /** * traverse all activity and finish * * @param isdirectexit * Isdirectexit whether to exit */public void Exit (Boolean Isdirectex It) {if ((System.currenttimemillis ()-Mexittime) >) {toast.maketext (this, "Press again to exit the program", Toast.length_short). Show (); mexittime = System.currenttimemillis ();} else {if (isdirectexit) {///exit other apps for (Activity activity:activitylist) {LOG.I (tag, "Activity.getpackagenamE (): "+ activity.getpackagename ()); Activity.isfinishing ())) {activity.finish ();}} Activitylist.clear ();//Stop Log service LOG.D (tag, "stopping log Srvice");//determine SDK version int sdkversion = Integer.parseint (version. SDK), if (sdkversion >= 8) {android.os.Process.killProcess (Android.os.Process.myPid ());} else {Activitymanager Activitymgr = (Activitymanager) this.getsystemservice (Activity_service); Activitymgr.restartpackage (getPackageName ());} Jump to Desktop Intent intent = new Intent (Intent.action_main); Intent.setflags (Intent.flag_activity_new_task); Intent.addcategory (Intent.category_home); startactivity (intent); System.exit (0);} else {exit ();}}} /** * traverse all activity and finish */public void exit () {if ((System.currenttimemillis ()-Mexittime) >) {toast.maketext (t His, "Press the Exit program again", Toast.length_short). Show (); mexittime = System.currenttimemillis ();} else {//exit other apps for (Activity activity:activitylist) {LOG.I (tag, "Activity.getpackagename ():" + Activity.getclassloader (). GetClass (). GetName ());//if (!) ( ActivitY.isfinishing ())) {activity.finish ();//}}activitylist.clear ();//Stop Log service LOG.D (tag, "stopping log Srvice");// Determine the SDK version int sdkversion = Integer.parseint (version. SDK), if (sdkversion >= 8) {android.os.Process.killProcess (Android.os.Process.myPid ());} else {Activitymanager Activitymgr = (Activitymanager) this.getsystemservice (Activity_service); Activitymgr.restartpackage (getPackageName ());} Jump to Desktop Intent intent = new Intent (Intent.action_main); Intent.setflags (Intent.flag_activity_new_task); Intent.addcategory (Intent.category_home); startactivity (intent); System.exit (0);}} /** * Return to Desktop */public void Backtohome () {//Jump to Desktop intent intent = new Intent (Intent.action_main); Intent.setflags (Intent.flag _activity_new_task); intent.addcategory (intent.category_home); startactivity (intent);} /** * Remove activity from container * * @param activity */public void removeactivity (activity activity) {activitylist.remove (activity);} /** * Clear all Activity */public void removeallactivity () {//Exit other apps for (Activity Activity:activitylist){LOG.I (tag, "Activity.getpackagename ():" + Activity.getclassloader (). GetClass (). GetName ()); Activity.isfinishing ())) {activity.finish ();//}}activitylist.clear ();} /** * Determine if the activity exists in the container * * @param activity * @return */public Boolean isactivitycontain (activity activity) {return act Ivitylist.contains (activity);} @Overridepublic void OnCreate () {super.oncreate (); appContext = Getapplicationcontext ();}}