android - 如何关闭 Android 中的所有 Activity 并从任务管理器中关闭?

标签 android android-intent android-activity

我想要关闭安卓应用

int id= android.os.Process.myPid();
android.os.Process.killProcess(id);

此代码终止进程。

最佳答案

如果每次用户从主屏幕启动应用程序或从最近使用的应用程序列表返回时,您总是希望您的应用程序从根 Activity(第一个 Activity)开始,则将其添加到<activity>根 Activity 的标签:

 android:clearTaskOnLaunch="true"

但是,如果您只想完成当前任务中的所有 Activity ,您可以执行以下操作:

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

这将导致任务中的所有 Activity 完成,并创建一个新实例 MyRootActivity .在 MyRootActivity.onCreate()执行以下操作:

if (getIntent().hasExtra("exit")) {
    finish(); // We want to exit the application, so just finish this activity now
}

关于android - 如何关闭 Android 中的所有 Activity 并从任务管理器中关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14052849/

相关文章:

android - 无法更改从 Firebase 控制台发送的 Firebase 云消息传递通知的默认图标

android - 按下后退按钮两次后完成 Activity ?

选项菜单中的 Android startActivity 导致应用程序崩溃

android - 是否有适用于 android 的 super 简单 List/ListAdapter 示例

android - 在 android 中点击一个 get/post 请求一个 URL 但在服务器端没有收到请求

Android SQLite 外键语法错误

android - 在 RecyclerView 上膨胀类 ImageView 时出错

android - 针对此设计更正 Android 布局

android - ACTION_LOCATION_SOURCE_SETTINGS : cannot be resolved or is not a field

java - 从 Activity 上下文外部调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志。这真的是你想要的吗?