Several ways Android safely exits the application

Source: Internet
Author: User

Several ways Android safely exits the application

To close the application gracefully:

When your app is no longer in use, you usually need to close the app, and you can turn off the Android app in three ways:

The first method: Get the ID of the current process first, and then kill the process.
Android.os.Process.killProcess (Android.os.Process.myPid ())
The application cannot be completely killed in this way, and he will not empty the task stack of the activity that the application is currently applying to

Second method: Terminates the currently running Java Virtual machine, causing the program to terminate
System.exit (0);
Don't kill your own app

The third approach: forcing all executions that are associated with the package to be closed
Activitymanager manager = (Activitymanager) getsystemservice (Context.activity_service);
Manager.restartpackage (Getpackagename ());
<uses-permission android:name= "Android.permission.RESTART_PACKAGES"/>

You can only kill others and not kill yourself.

practices in the enterprise:
Create Class App inheritance application

Androidmainfest-->name-->app//declares that the class adds android:name= ". Global instance App" for the entire application global instance

Create an instance of the entire application global
App.class:
Arraylist<activity> activities;

In the other activity:
public void OnCreate (Bundle savedinstancestate) {
App app = (APP) getapplication ();//Get Application Global Instance reference
App.activities.add (this); Put the current activity into the collection
}

public void Ondestory () {
App app = (APP) getapplication ();//Get Application Global Instance reference
App.activities.remove (this); Remove the current activity from the collection
}


Safe exit:
The following code is defined in the event of the menu Exit button:
App app = (APP) getapplication ();
list<activity> activities = app.activities;
for (Activity act:activities) {
Act.finish ();//Explicit end
}

Several ways Android safely exits the application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.