How Android exits the application of multiple activities

During the development process, we often need an exit function to exit all activities of the application. Below, we list several ways to exit the application.

1. Use activitycontainer to manage the references of all activities

2. Use the broadcast to notify the end of baseactivity

3. Kill the process directly

4. Use the characteristics of singletask to end the application

5. More elegant implementation of the fourth method

6. Double click the return key to exit

Method 1: use activitycontainer to manage all activity references

This method is to create a generic activity's parent class baseactivity and an activitycontainer class. Activitycontainer is a collection class to store references to all activities. The key codes are as follows:

Then, the activities of the real business requirements inherit from baseactivity respectively, and execute when it is necessary to exit the application

Method 2: use the broadcast to notify the end of baseactivity

Create a broadcast receiver based on the original baseactivity. When receiving the broadcast notification, execute baseactivity. This. Finish();

The code of baseactivity is as follows:

Method 3: directly kill the process

This method kills the process where the current activity is located through the process killing method provided by the system. However, due to too many modifications to the bottom layer by various custom ROMs, this method can not work in many cases

Method 1: Android. OS. Process. Killprocess (Android. OS. Process. Mypid());

Mode 2: system.exit (0);

Method 3: activitymanager = (activitymanager) getsystemservice (activity_service); manager.killBackgroundProcesses(getPackageName());

Method 4: use the characteristics of singletask to end the application

The general Activity open sequence of our application is the initial splash page, and then transferred to our main Activity. At the same time, finish drops the splash page, and then performs various Activity jumps on the main Activity. These jump Activity, some finish drop, some in the task stack. If we set the startup mode of the main activity as singletask, and then the startup mode of the activities started from this activity is the default mode, these activties will be in the same task stack as the main activity, and the main activity will be at the bottom of the stack. At this time, when we exit, start the main activity and send a broadcast to the main activity to exit the activity. Because of the characteristics of singletask, all activities above all activities will be cleared. The broadcast will notify the main activity to end itself, and all activities will be completely exited.

The specific steps are as follows:

1. Register a broadcast in mainactivity. The broadcast content is mainactivity. This. Finish()

2. On the options page, click the exit button to start mainactivity. Because of the characteristics of singletask, other activities will be cleared. When starting, send a broadcast to mainactivity to end it.

3. Exit all applications

Mode 5: more elegant implementation of the fourth mode

The idea of the fourth method is correct, but the form of notifying mainactivity to exit through broadcasting is really not very good. For the implementation of activity, a better way is to use the onnewintent method.

This method will be called when the activity returns to the foreground from the background again. The complete method call is onnewintent onrestart onresume. We can implement the exit of mainactivity in onnewintent.

When clicking the exit button on the options page, when starting mainactivity, intent carries an exit parameter. When this parameter is detected, it exits the application.

Method 6: double click the return key to exit

Double clicking the exit button is a relatively simple and effective exit method. The interface more suitable for exit is mainactivity, which uses the singletask feature of mainactivity, but when you return to the foreground, clear other activities on it. At this time, listen for the Keydown event on mainactivity and double-click to exit. The code is as follows:

Here, a handler implements the function of a timer to ensure that it is within 2S, but if it is not returned, the state will be restored. There are many ways to implement this timer, such as timer, as follows:

Or simply calculate the time difference. This method is too simple to say more.

Source address: activityquitdemo_ jb51.rar

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
分享
二维码
< <上一篇
下一篇>>