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
To create visually appealing apps, displaying ima...
Reservoir It is a flood control project trump car...
The third video + PDF resource introduction of th...
Recently, a boy in Nanyang, Henan Province was bi...
First, let me give you a few tips: Tip 1: How to ...
Samsung accounts for one-fifth of South Korea'...
Private domain marketing is not a new term, but p...
There is a good news or a bad news. Which one do ...
At around 23:00 on April 7, 2021, my country'...
Yunnan's Ailao Mountain unexpectedly became p...
Today, China's cross-border e-commerce is dev...
Introduction to conversion rate thinking resource...
1. What is Kuaishou ? 1. Basic Introduction Type:...
There is more and more consensus on doing private...
Expert of this article: Liu Yan, Space Applicatio...