如何关闭Android中的所有活动并从任务管理器关闭? [英] How to close all activities in Android and close from Task Manager?

查看:262
本文介绍了如何关闭Android中的所有活动并从任务管理器关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要关闭android应用

I want close android app

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

此代码终止进程.

推荐答案

如果每次用户从主屏幕启动该应用程序或从该应用程序返回时,总是希望您的应用程序以root活动(第一个活动)开始最近应用程序的列表,然后将其添加到您根活动的< activity> 标记中的清单中:

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

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

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
}

这篇关于如何关闭Android中的所有活动并从任务管理器关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆