Although the document is not clearly stated, but if this is a common function, there should be an easy way to implement, and actually rely on code to "exit" an application is not easy. The following is a summary of the two scenarios that can simulate the exit effect:
Scenario 1: Open the System Master screen to simulate the effect of the application exit, which is no different from the user pressing the home key.
The code is as follows |
Copy Code |
Intent Intent = new Intent (intent.action_main); Intent.addcategory (Intent.category_home); Intent.setflags (Intent.flag_activity_new_task); StartActivity (Intent); |
Scenario 2: Kill the current application process directly. This method is too violent, I found a piece of iOS development document, which strongly does not recommend the way to kill the process to exit the application, because it also applies to the Android system: the effect of the exit is easy to let users think that the application crashes.
The code is as follows |
Copy Code |
int Pid=android.os.process.mypid (); Android.os.Process.killProcess (PID); |
In addition, it was suggested to call System.exit (0) to exit the application, and the actual test found that the method often only closes the current activity or does not work at all.
From this, we can see that there is no "exit application" mechanism in the design of the Android system, when the user presses the home key or presses the back key in the application homepage, the application is placed in the background, and when the application process is to be completely killed, the system decides. Android and iOS have abandoned the concept of "quit apps," and for mobile users, he just needs to know "start apps"-the less the concept, the simpler it is.
But as a good application, should have its own exit function.
"Application Exit" generally has the following methods:
An activity that exits from the beginning, so that the activity system behind will destory itself. (usually takes a long time)
Write a onkeydown () method for each activity, and finish off the current activity each time you press the return key. (usually all of the activity will inherit a baseactivity, so just write it once.)
Third, use list, each call new activity, save the current activity, when the application exits, one-time finish all activity. (More ideal solution)
Today, we use a third way to add the "exit" feature to our application.
1. Write a class inheritance application
The code is as follows |
Copy Code |
public class Appclose extends application { Private list<activity> mainactivity = new arraylist<activity> (); Public list<activity> mainactivity () { return mainactivity; } public void Addactivity (activity Act) { Mainactivity.add (ACT); } public void Finishall () { For (activity act:mainactivity) { if (!act.isfinishing ()) { Act.finish (); } } Mainactivity = null; } } |
2. Add in the OnCreate method in the activity
The code is as follows |
Copy Code |
Appclose appstate = (appclose) this.getapplication (); Appstate.addactivity (this); Setcontentview (R.layout.main); |
3.//Set Click event
The code is as follows |
Copy Code |
Button.setonclick. () { Appclose appstate = (appclose) getapplicationcontext (); Appstate.finishall (); } |
4. In the registry document
Add attribute android:name= to application. Appclose "