I have always had a misunderstanding about application exit. I once thought that finish () is the end of an Activity, System. exit (0) ends the entire Application until one day ..... --------------------- I am a gorgeous split line -------------------- this is a thrilling story once, an android app is like this: Layout file of the main Activity: Copy code 1 <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" 2 xmlns: tools = "http://schemas.android. Com/tools "3 android: layout_width =" match_parent "4 android: layout_height =" match_parent "5 android: paddingBottom =" @ dimen/activity_vertical_margin "6 android: paddingLeft = "@ dimen/activity_horizontal_margin" 7 android: paddingRight = "@ dimen/plugin" 8 android: paddingTop = "@ dimen/activity_vertical_margin" 9 tools: context = ". mainActivity "> 10 11 <TextView12 android: id =" @ + id/txt "13 android: Layout_width = "match_parent" 14 android: layout_height = "wrap_content" 15 android: layout_alignParentTop = "true" 16 android: text = "this is the first activity"/> 17 18 <Button19 android: id = "@ + id/btn" 20 android: layout_width = "wrap_content" 21 android: layout_height = "wrap_content" 22 android: layout_below = "@ + id/txt" 23 android: layout_centerHorizontal = "true" 24 android: onClick = "btnClick" 25 android: text = "jump"/> 26 27 </RelativeLayout> copy the code. Do not pay attention to the nonstandard details. This is just a story! Then Activity (Launcher) looks like this: Copy code 1 public class MainActivity extends Activity {2 3 @ Override 4 protected void onCreate (Bundle savedInstanceState) {5 super. onCreate (savedInstanceState); 6 setContentView (R. layout. activity_main); 7} 8 9 public void btnClick (View v) {10 Intent intent = new Intent (MainActivity. this, SecondActivity. class); 11 startActivity (intent); 12 this. finish (); // pay attention to this place. The next step is to witness the miracle !!! 13} 14 15} copy code we can see that we also need a jump Activity, here: copy the code public class SecondActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState); LinearLayout layout = new LinearLayout (this); TextView txt = new TextView (this); txt. setText ("this is the second activity"); Button btn = new Button (this); btn. setText ("exit"); bt N. setOnClickListener (new OnClickListener () {@ Override public void onClick (View arg0) {// TODO Auto-generated method stub System. exit (0) ;}}); layout. addView (txt); layout. addView (btn); setContentView (layout) ;}} the code has been copied, and an android Application with the simplest redirection and disabling functions has been completed. Below is: this seems to verify the System. the validity of exit (0). However, let's go back and read the Activity (Launcher) code. Right, just add the comment. What will happen if we remove it? The following is the time to witness the miracle: System. exit (0) is invalid !!!! -------------------------------------------- The story has been told. -------------------- why is this effect true? This is because finish (): ends the current Activity and does not release the memory immediately. Follows the android memory management mechanism. Follow the lifecycle of the activity. System. exit (): ends the current component, such as Activity, and immediately releases the resources occupied by the current Activity. Does not follow the lifecycle of the activity. In fact, I have doubts here. android is a process and a virtual machine, and the activity of the same application is in the same process by default. exit (0) (parameter 0 indicates normal exit) is used to terminate the currently running Virtual Machine. why can't the entire application be terminated? If you are familiar with it, you may wish to provide guidance. Speaking of this, we need to consider how to exit the whole application on the front-end activity without finishing the background activity? (Here, we do not consider releasing memory, Service, and other components, and a series of operations that may need to be processed in the activity lifecycle.) There are two popular reliable methods: one is to create a custom stack to manage the activity in singleton mode, implement the exit () method to traverse all the activities and finish (), but I don't like it and have never used it, so you can search on the Internet if you need it. There are still a lot of examples. Here I will introduce the second method. After creating the base class BaseActivity of the activity, exit. The specific implementation is as follows: first create a base class BaseActivity copy code 1 public class BaseActivity extends Activity {2 public Activity activity; 3 public ExitAllBroadCast exitAllBroadCast; 4 5 @ Override 6 protected void onCreate (Bundle savedInstanceState) {7 super. onCreate (savedInstanceState); 8 activity = BaseActivity. this; 9 exitAllBroadCast = new ExitAllBroadCast (); 10 11} 12 13 @ Override14 protected void onStart () {15 IntentFi Lter filter = new IntentFilter (); // creates the IntentFilter object 16 filter. addAction ("com. all. exit. broadcast "); 17 registerReceiver (exitAllBroadCast, filter); // register Broadcast Receiver18 super. onStart (); 19} 20 class ExitAllBroadCast extends BroadcastReceiver {21 22 @ Override23 public void onReceive (Context context, Intent intent) {24 25 activity. finish (); 26} 27 28} 29 @ Override30 protected void onDestroy () {31 32 s Uper. onDestroy (); 33 unregisterReceiver (exitAllBroadCast); 34} 35} copy the code in the base class. We registered a broadcast receiver. When "com. all. exit. when broadcasting broadcast, execute finish (). Now, let all the activities inherit this BaseActivity. When we want to exit, it is better to send a broadcast. 1 Intent myIntent = new Intent (); // create Intent object 2 myIntent. setAction ("com. all. exit. broadcast "); 3 sendBroadcast (myIntent); // send broadcast