The Android program has a lot of activity, such as the main window A, called sub-window B, if the direct finish () in B, then the next show is a. How do I close the entire Android app in B? I have summed up several relatively simple implementation methods.
1. Local methods for Dalvik VMS
Android.os.Process.killProcess (Android.os.Process.myPid ())//Get PID
System.exit (0); Normal Java, C # Standard Exit method with a return value of 0 for graceful exit
2. Task Manager method
The first step is to show that the method runs on Android 1.5 API level 3 to be able, while requiring permissions
Activitymanager am = (activitymanager) getsystemservice (Context.activity_service);
Am.restartpackage (Getpackagename ());
System will, the package under, all processes, services, all killed, you can kill clean, to pay attention to add
<uses-permission android:name=\ "Android.permission.restart_packages\" ></uses-permission>
3. According to the activity's declaration cycle
3. We know that the Android window class provides a history stack, we can use the principle of the stack ingenious implementation, here we in a window open b window in the Intent to join the flag intent.flag_activity_clear_top directly, When you turn on B, all activity for that process space will be cleared.
In the a window, use the following code to invoke the b window
Intent Intent = new Intent ();
Intent.setclass (Android123.this, Cwj.class);
Intent.setflags (Intent.flag_activity_clear_top); Note the flag setting of the bank
StartActivity (Intent);
Next, in the b window you need to exit directly using the Finish method to exit all.
4. Customize a actiivty stack, as above, but use a single-mode activity stack to manage all activity. and provides a way to exit all activity. The code is as follows:
1 Public classScreenmanager {2 Private StaticStack<activity>Activitystack;3 Private StaticScreenmanager instance;4 PrivateScreenmanager () {5 }6 Public StaticScreenmanager Getscreenmanager () {7 if(instance==NULL){8Instance=NewScreenmanager ();9 }Ten returninstance; One } A //exiting the top of the stack activity - Public voidpopactivity (activity activity) { - if(activity!=NULL){ the activity.finish (); - activitystack.remove (activity); -activity=NULL; - } + } - + //get current stack top activity A PublicActivity currentactivity () { atActivity activity=activitystack.lastelement (); - returnactivity; - } - - //pushes the current activity into the stack - Public voidpushactivity (activity activity) { in if(activitystack==NULL){ -activitystack=NewStack<activity>(); to } + Activitystack.add (activity); - } the //exit all activity in the stack * Public voidPopallactivityexceptone (Class cls) { $ while(true){Panax NotoginsengActivity activity=currentactivity (); - if(activity==NULL){ the Break; + } A if(Activity.getclass (). Equals (CLS)) { the Break; + } - popactivity (activity); $ } $ } -}
Android-four ways to completely exit the current application