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 public is familiar with hybrid rice, but few ...
When we do bidding promotion, we will come into c...
[Introduction] DAU, GMV, CPM, CPC... This series ...
Your best "insider" in the scientific c...
For products, without effective exposure, they wi...
Wang Jing, Wang Chao, Shen Qiushi, Science and Te...
"Dishwashing liquid contains formaldehyde, w...
Recently, the "Automotive Industry Investmen...
Previously, Lexian Technology's "Celebri...
Professor Zhang Jingming's "Chinese Medi...
According to data from the National Bureau of Sta...
The year just passed There has been a lot of news...
Are "hardworking" and "working ove...
January 20 marks the arrival of the "Great C...
John Wei focuses on studying copyright issues in ...