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

How to efficiently display bitmaps on Android App

To create visually appealing apps, displaying ima...

Can you tell these lines of the reservoir apart?

Reservoir It is a flood control project trump car...

Seven Swords Zongming's Dragon Head Battle Method Part 3 Video + PDF

The third video + PDF resource introduction of th...

Practical tips: How to promote an event well?

First, let me give you a few tips: Tip 1: How to ...

Samsung Empire: The Crisis of the Authoritarian Model

Samsung accounts for one-fifth of South Korea'...

Analysis of Huaxizi and Winona’s private domain marketing!

Private domain marketing is not a new term, but p...

How can cross-border e-commerce gain brand advantages?

Today, China's cross-border e-commerce is dev...

Conversion rate thinking that everyone needs

Introduction to conversion rate thinking resource...

5 stages to build a private community SOP from 0 to 1

There is more and more consensus on doing private...