Reference: http://johncookie.iteye.com/blog/890734
Android program has a lot of activity, such as the main window A, called sub-window B, sub-window B and call child window C,back back to sub-window B, in B How to close the entire Android application? Here are a few ways to use it.
1. Finish () method
Finish is the activity class, only for the activity, when the call to finish (), only the activity is pushed back to the background, and does not immediately release memory, the active resource is not cleaned up; the call to the finish () method executes Activity.ondestroy ( ) method to end the activity life cycle
In the development of Android applications, often by pressing the return key (ie keycode = = keyevent.keycode_back) can close the program, in fact, most of the time the application is still running in the task, in fact, this is not the result we want.
2. Local methods for Dalvik VMS
android.os.Process.killProcess (Android.os.Process.myPid ()); Get the PID, now get their own also only the API, otherwise from/PROC in the enumeration of other processes, but to note that the end of other processes do not necessarily have permissions, or chaos.
system.exit (0); normal Java, C # Standard Exit method, the return value of 0 represents a normal exit, can be written in the Ondestory method.
We can do this, when the user clicks on the Custom Exit button or return key (need to capture the action), we force exit the application in OnDestroy (), or directly kill the process, the specific operation code is as follows:
Capturing keyboard actions
@Override
public boolean onKeyDown (int keycode, keyevent event) {
When you press the Back button on the keyboard, give the Exit dialog box
if (keycode = = Keyevent.keycode_back) {
New Alertdialog.builder (This)
. SetIcon (R.drawable.services)
. Settitle (R.string.prompt)
. Setmessage (R.STRING.QUIT_DESC)
. Setnegativebutton (R.string.cancel, New Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
}
})
. Setpositivebutton (R.string.confirm, New Dialoginterface.onclicklistener () {
public void OnClick (dialoginterface dialog, int whichbutton) {
Finish ();//
}
}). Show ();
return true;
}else{
Return Super.onkeydown (KeyCode, event);
}
}
@Override
protected void OnDestroy () {
Super.ondestroy ();
System.exit (0);//Direct End Program
or the following way
Android.os.Process.killProcess (Android.os.Process.myPid ());
}
3. Task Manager method
First of all to demonstrate that the method runs on Android 1.5 API level of more than 3 can, while requiring permission Android.permission.RESTART_PACKAGES, We directly end their package can.
Directly using the Activitymanager class's Restartpackage method, the parameter is package name, which is passed Getsystemservice (context.activity_service) To instantiate the Activitymanager object, which is provided by the system but needs to be displayed with declarative permissions, so it needs to be considered in a comprehensive way.
Focus on this approach:
Activitymanager manager = (Activitymanager) getsystemservice (Activity_service);
Manager.restartpackage (Getpackagename ());
Need to declare permissions
This method terminates everything associated with this package, all processes that share the same UID are killed, and all activity is removed
All created services stop, and a broadcast intent.action_package_restarted causes all registrations alarms to be stopped, notifications is removed.
But a new API has been added to Android 2.2 to help us kill the background process (Android2.3 again emphasizes its call to the API level at least 8) Killbackgroundprocesses is a method of the Android.app.ActivityManager class that must be added to the Androidmanifest.xml file when used Kill_background_ Processes this permission. Although this class also provides an API level of 3 for the Restartpackage (String PackageName) method call, the SDK has been marked as deprecated, but the principle is the same. But in the past, Google's naming method is really not very reasonable, the role of restartpackage it is difficult for us to associate with the end process.
The prototype of the method:
public void killbackgroundprocesses (String packagename) has only one parameter for package Name, which is relatively simple to use.
Activitymanager am = (activitymanager) getsystemservice (Activity_service);
Am.killbackgroundprocesses ("CN.COM.ANDROID123.CWJ"); API level of at least 8 to use
You must include kill_background_processes this permission in the Androidmanifest.xml file.
Therefore, it is best to use this method to make a judgment if the ROM before 2.2 with Restartpackage, then the use of killbackgroundprocesses.
Activitymanager am = (activitymanager) getsystemservice (Activity_service);
if (Android.os.Build.VERSION.SDK_INT < 8) {
Am. Restartpackage (Getpackagename ());
}else{
Am.killbackgroundprocesses (getpackagename ());
}
Reference:
Http://dev.10086.cn/cmdn/wiki/index.php?doc-view-6270.html
Http://www.cnblogs.com/275095923/archive/2011/09/05/2167958.html
Http://hi.baidu.com/jackxiangzi/item/938df16e3aec060fa1cf0fcc
4. According to the activity's declaration cycle
We know that the Android window class provides a history stack, which we can skillfully implement using the stack principle, where we add the flag intent.flag_activity_clear_top directly to the intent when opening the b window in a window. 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.
Several methods of exiting the program in Android development