Method developed by Android to close all activities

When multiple activities are opened in the app, due to the deep entry, many apps have to let users return to the first interface (mainactivity) step by step. When returning step by step, they release them one by one.

Today, write kotlin to realize the method of directly exiting the application at any position.

First, whenever an activity is opened, there needs to be a place to record so that a loop traversal code can release all used activity resources.

So, create an array. Mutablelist is used here because it has add and remove methods, which is easy to use.

This array is used to record how many activities are currently open.

Then each time you open a new activity, you need to perform an action to add to the array.

Then write the core operation and release all activities.

The first is to traverse the array. The traversed activity determines whether it is null. If it is not null, it proves that it has not been released. Then finish the activity. Then remove it from the array.

This is not over yet. You need to add the array when each activity oncreate

It is written in the override fun oncreate (savedinstancestate: bundle?) method.

Of course, this is not enough. You also need to implement the removal function for a separate activity.

In this way, it has basic functions.

PS: other activity method closing method

Four methods to end the current activity

But if you have started four activities: A, B, C and D,

In D activity, you want to start another activity B, but instead of becoming a, B, C, D and B, you want a and B, and the data on B remains

If four activities have been started: A, B, C and D,

In D activity, you want to start another activity B, but it does not become a and B, and the data on B is not retained

If you have started four activities: A, B, C and D, and want to start another activity B in D activity, but it does not become a and B, you can write code as follows:

If you have started four activities: A, B, C and D, in D activity, you want to close all activities at once

Create a class dedicated to handling activities

Add when each activity is created

Add the activity to the list.

When you want to close, call the exit method of sysapplication

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>