How to safely exit multiple Activities on Android

How to safely exit multiple Activities on Android

When making Android Apps, almost every App has a "Quit App" function. How can I completely exit an App? I searched some articles on Google. They all cached Activities through List. When you need to exit the App, iterate the activity list and call the finish method. This solution can completely exit the App, but it has a big potential problem. Since the Activity is cached and cannot be released, it is easy to cause OOM. Therefore, this method should be used with caution.

The following is a solution that uses the flag attribute when the Activity is started to solve this problem. Its performance is better than the solution of completely exiting the App by caching the Activity, but this solution needs to meet some conditions (see the analysis process).

Implementation steps:

1: Create an "empty" auxiliary Activity and call the finish method in its onCreate method. As follows:

  1. public   class LastActivity extends Activity {
  2.  
  3. @Override  
  4. protected   void onCreate(Bundle savedInstanceState) {
  5. super .onCreate(savedInstanceState);
  6. finish();
  7. }
  8. }

2: Click the "Exit App" button to execute the following method:

  1. findViewById(R.id.quit).setOnClickListener( new OnClickListener() {
  2.  
  3. @Override  
  4. public   void onClick(View v) {
  5. Intent intent = new Intent(SecondActivity. this ,LastActivity. class );
  6. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
  7. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  8. SecondActivity. this .startActivity(intent);
  9. finish();
  10. }
  11. });

These two steps can be used to exit the entire application. Here is a simple analysis:

Three Activities are used in the test, and the execution order is MainActivity -> SecondActivity -> LastActivity. In SecondActivity, we expect to click the "Exit app" button to completely exit the App. At this time, we will use an auxiliary Activity (LastActivity).

First, after the click event is triggered, the LastActivity operation is executed in the onClick method. Before executing the click event, we first check the running Activity in the task stack, which can be viewed through the following command:

adb shell dumpsys activity

The results are as follows:
At this point we can know that MainActivity and SecondActivity belong to the same task stack, and the startup order is MainActivity -> SecondActivity. In the onClick method, when we are ready to start LastActivity, its Flag is set to Intent.FLAG_ACTIVITY_CLEAR_TASK. The description of this flag in the Android document is "If set in an Intent passed to Context.startActivity(), this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started." It means that when starting a new Activity, the task stack "related" to this Activity will be cleared. At this time, except for the Activity (SecondActivity) in the stack itself, all will be cleared, and then the Activity (LastActivity) will be started. Let's verify whether it is correct. Add a breakpoint before the onCreate call finish of LastActivity, and then click the "Exit App" button to view the task stack information as follows:
At this point we can see that after LastActivity is started, MainActivity is cleaned up, and only LastActivity and SecondActivity remain in the task stack. Then the finish method of LastActivity is run, LastActivity exits, and then SecondActivty also exits, and the App is exited.

The above method is for all activities to be in the same task stack. Can different activities with different task stacks also completely exit the App? Next, we will make the following settings in the AndroidManifest.xml file for SecondActivity and LastActivity to place them in different task stacks:

  1. <activity
  2. android:name= ".SecondActivity"  
  3. android:launchMode= "singleTask"  
  4. android:taskAffinity= "com.umeng.social"  
  5. />
  6. <activity
  7. android:name= ".LastActivity"  
  8. android:launchMode= "singleTask"  
  9. android:taskAffinity= "com.umeng.social.test"  
  10. />

At this point we check the task stack as follows:
At this point we can see that MainActivity and SecondActivity are in different task stacks. After clicking the Exit App button, the Activity stack is as follows:
It can be seen that the three Activities are in different task stacks. Careful students may have discovered the problem, why LastActivity has been started, but MainActivity has not been destroyed? Yes, this is the special meaning of the word "related" in the above-mentioned 'clean up the task stack "related" to this Activity', which means that there are conditions when cleaning up the Activity, and the condition is "the cleaned Activity must be in the same task stack as the Activity to be started". Since MainActivity, SecondActivity, and LastActivity are now in different task stacks, the Activity will not be cleaned up. Execute the subsequent code of the breakpoint, and MainActivity still exists in the Activity stack, resulting in the entire App cannot be completely exited.

Therefore, there are conditions for completely exiting the App by setting a flag. The conditions are: "The entire App's Activities exist in the same task stack" or the task stack model meets the following conditions:
At present, there is no better solution for completely exiting the App when there are multiple task stacks. If anyone knows, please leave a message. Thank you.

<<:  Talking about Android security 2 - Activity hijacking prevention program

>>:  Android Activity Security

Recommend

"Sacred Salt" and "Salt God": The Mysterious Salt Worship

The magical power that salt bestows on mankind an...

The world's largest gambling city: I advise you not to gamble

today Let's go to Las Vegas, known as the &qu...

A look at the tech products and services that disappeared in 2015

[[161053]] 1. Amazon Fire Phone Amazon's Fire...

3 issues in ToB product operation!

When I was doing ToB operations before, I treated...

What was that flying by? It was our vacation.

Audit expert: Chen Mingxin National Level 2 Psych...

Drawing Chun Ge-Character Sketch: Practical Stylized Character Creation

Course Catalog ├──Lesson Notes| └──1 | | ├──B Stat...

Apple Watch battery life is only one day? Apple says it will change

Apple did not disclose battery life for the Apple...

Cognitive neuroscience: self and social intelligence

Self and Social Intelligence ——A person has many ...