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
The mobile game market seems to be booming, but m...
Contributing author: Su Hao (Professor at North C...
[[163210]] What is a BUG? To put it simply, the p...
Marketing genius John Wanamaker has a famous sayi...
On February 22, at Guangzhou Port, 300 units of X...
Excerpted from: Inside and Outside the Classroom ...
If you eat mushrooms well, you will reach paradis...
...
Website navigation is very important for a websit...
Fingerprints have become almost synonymous with b...
There are many website statistics tools, such as ...
How much does it cost to produce the Huai'an ...
What are the methods of Internet marketing? You c...
In order to adapt to iPhone X and iPad Pro, iOS h...
Baidu has launched a new program called "Apo...