Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to safely exit multiple Activity in Android

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to safely exit multiple Activity in Android". In daily operation, I believe that many people have doubts about how to safely exit multiple Activity in Android. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to safely exit multiple Activity in Android". Next, please follow the editor to study!

Implementation steps:

1: create an "empty" secondary Activity and call the finish method in its onCreate method. As follows:

Public class LastActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); finish ();}}

2: click the "exit App" button to perform the following methods:

FindViewById (R.id.quit) .setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {Intent intent = new Intent (SecondActivity.this,LastActivity.class); intent.addFlags (Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK); SecondActivity.this.startActivity (intent); finish ();}})

Through these two steps, you can exit the whole application. Here's a simple analysis:

Three Activity 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 function, at this time we will use an auxiliary Activity (LastActivity).

First, after the click event is triggered, the start LastActivity operation is performed in the onClick method. Before executing the click event, let's check the Activity running in the task stack, which can be viewed with 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, and the flag is expressed as "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." in the android document, which means that when a new Activity is started, the task stack "associated" with this Activity will be cleaned up. At this point, except for the Activity (SecondActivity) in the stack itself, it will be cleaned, and then start Activity (LastActivity). To verify that this is correct, add a breakpoint before the onCreate of LastActivity calls finish, and then click the "exit App" button to view the information of the task stack as follows:

At this point, we can see that after LastActivity is started, MainActivity is cleaned, and only LastActivity and SecondActivity are left in the task stack. Then run the finish method of LastActivity, LastActivity exits, then SecondActivty exits, and then App exits.

The above approach is that all Activity exist in the same task stack. Can different Activity with different task stacks really exit App completely? Next, we will make the following settings for SecondActivity and LastActivity in the AndroidManifest.xml file so that they are on different task stacks:

At this point, let's take a look at the task stack as follows:

At this time, we can see that MainActivity and SecondActivity are in different task stacks. After clicking the exit App button, the Activity stack of the Activity stack is as follows:

You can see that the three Activity are in different task stacks at this time. Careful students may have found the problem, why LastActivity has been started, but MainActivity has not been destroyed? Yes, this is the special meaning of the word "related" in "cleaning up the task stack related to this Activity" mentioned above, which indicates that there is a condition when cleaning up the Activity, the condition is that "the Activity being clean must be in the same task stack as the Activity to be started". Because MainActivity, SecondActivity, and LastActivity are now in different task stacks, Activity will not be cleaned up. When the subsequent code of the breakpoint is executed, there is still a MainActivity in the Activity stack, so that the entire App cannot be exited completely.

At this point, the study on "how to safely exit multiple Activity in Android" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report