How to close all activities in Android and close from Task Manager?

送分小仙女□ 提交于 2021-01-28 05:57:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!