如何检查活动是否是应用程序活动堆栈中的最后一个活动? [英] How to check if an activity is the last one in the activity stack for an application?

查看:20
本文介绍了如何检查活动是否是应用程序活动堆栈中的最后一个活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如果用户退出当前活动是否会返回主屏幕.

I want to know if user would return to the home screen if he exit the current activity.

推荐答案

更新(2015 年 7 月):

由于 getRunningTasks() 被弃用,从 API 21 开始,最好遵循 raukodraug answerEd Burnette> 一个(我更喜欢第二个).

Since getRunningTasks() get deprecated, from API 21 it's better to follow raukodraug answer or Ed Burnette one (I would prefer second one).


可以使用 ActivityManager.

因此,要确定一项活动是否是最后一项:

So, to determine if an activity is the last one:

  • 在清单中请求 android.permission.GET_TASKS 权限.
  • 使用以下代码:

  • request android.permission.GET_TASKS permissions in the manifest.
  • Use the following code:

ActivityManager mngr = (ActivityManager) getSystemService( ACTIVITY_SERVICE );

List<ActivityManager.RunningTaskInfo> taskList = mngr.getRunningTasks(10);

if(taskList.get(0).numActivities == 1 &&
   taskList.get(0).topActivity.getClassName().equals(this.getClass().getName())) {
    Log.i(TAG, "This is last activity in the stack");
}

请注意,以上代码仅在您有单个任务时才有效.如果您的应用程序可能存在多个任务 - 您需要检查其他 taskList 元素.阅读有关任务的更多信息任务和返回堆栈

Please note, that above code will be valid only if You have single task. If there's possibility that number of tasks will exist for Your application - You'll need to check other taskList elements. Read more about tasks Tasks and Back Stack


这篇关于如何检查活动是否是应用程序活动堆栈中的最后一个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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