问题
I want close android app
int id= android.os.Process.myPid();
android.os.Process.killProcess(id);
This code kills the Process.
回答1:
If you always want your app to start with the root activity (the first activity) every time the user launches the app from the home screen or returns to it from the list of recent apps, then add this to the manifest in the <activity>
tag for your root activity:
android:clearTaskOnLaunch="true"
However, if you want to just finish all activities in the current task you can do the following:
Intent = new Intent(this, MyRootActivity.class); // MyRootActivity should be the name of your root (launcher) activity
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("exit", "true"); // This tells MyRootActivity that you want to exit
This will cause all activities in the task to be finished and it will create a new instance of MyRootActivity
. In MyRootActivity.onCreate()
do the following:
if (getIntent().hasExtra("exit")) {
finish(); // We want to exit the application, so just finish this activity now
}
回答2:
this.finish();
Intent intent = new Intent(getApplicationContext(), CloseApp.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This code works for me. This will close all the existing activities as well as when reopen starts from first activity.
来源:https://stackoverflow.com/questions/14052849/how-to-close-all-activities-in-android-and-close-from-task-manager