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:
2: Click the "Exit App" button to execute the following method:
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: 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:
At this point we check the task stack as follows: 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: |
<<: Talking about Android security 2 - Activity hijacking prevention program
1. Product Background 1. Product Introduction Net...
From 0:00 to 24:00 on December 18, 83 new confirm...
Source code introduction: city selection controll...
Baby's height This has always been a concern ...
This article is quite long, and it more or less c...
After half a month of preparation, I have been th...
The North Star refers to the absolute core indica...
Hong Kong media recently pointed out that many st...
First FAQ Q1 What information do I need to submit...
What can make young people quit drinking coffee? ...
On May 7, Didi launched the Didi Express project ...
After the college entrance examination every year...
The two largest mobile operating systems, Android...
Humans have been looking for ways to measure temp...
With the development of social economy and change...