如何检查某个活动是否是应用程序活动堆栈中的最后一个活动?
我想知道如果用户退出当前活动是否会返回主屏幕。
I want to know if user would return to the home screen if he exit the current activity.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
我将改进 @H9kDroid 的评论,作为有类似问题的人的最佳答案。 (原始链接)
您可以使用 isTaskRoot() 了解活动是否是任务的根。
I'm going to improve on the comment of @H9kDroid as the best answer here for people that have a similar question. (Original link)
You can use isTaskRoot() to know whether the activity is the root of a task.
更新(2015 年 7 月):
自 getRunningTasks() 已被弃用,从 API 21 开始,最好遵循 raukodraug 答案或Ed Burnette 一个(我更喜欢第二个)。
可以使用 ActivityManager 检查当前任务及其堆栈。
因此,要确定某个活动是否是最后一个活动:
使用以下代码:
请注意,上述代码仅在您有单个任务时才有效。如果您的应用程序可能存在一定数量的任务 - 您需要检查其他 taskList 元素。详细了解任务任务和返回堆栈
< br>
UPDATE (Jul 2015):
Since getRunningTasks() get deprecated, from API 21 it's better to follow raukodraug answer or Ed Burnette one (I would prefer second one).
There's possibility to check current tasks and their stack using ActivityManager.
So, to determine if an activity is the last one:
Use the following code:
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
希望这对新初学者有所帮助,基于上面对我有用的答案,我还分享了代码片段,因此它很容易实现。
解决方案:我使用了
isTaskRoot()
,如果当前活动只是堆栈中的活动并且除了其他活动之外,它会返回true我还处理这样的情况:如果我在堆栈中有一些活动,则转到堆栈中的最后一个活动,而不是打开新的自定义活动。在您的活动中
Hope this will help new beginners, Based above answers which works for me fine, i am also sharing code snippet so it will be easy to implement.
solution : i used
isTaskRoot()
which return true if current activity is only activity in your stack and other than i also handle case in which if i have some activity in stack go to last activity in stack instead of opening new custom one.In your activity
有一个最简单的解决方案,您可以使用 isTaskRoot()< /a>
在你的活动中
there is an easiest solution to this, you can use isTaskRoot()
in your activity
跟踪此情况的一种方法是在开始新活动时包含一个标记,并检查该标记是否存在。
每当您开始一项新活动时,请插入标记:
然后您可以像这样检查它:
One way to keep track of this is to include a marker when you start a new activity and check if the marker exists.
Whenever you start a new activity, insert the marker:
And you can then check for it like this:
虽然可能有办法实现这一点(请参阅其他答案),但我建议您不要这样做。普通的 Android 应用程序不需要知道主屏幕是否即将显示。
如果您尝试保存数据,请将数据保存代码放入 onPause() 方法中。如果您试图为用户提供一种方法来改变他们对现有应用程序的想法,您可以拦截 Back 键的向上/向下键和 onBackPressed() 方法,并向他们显示“您确定吗?”迅速的。
While there may be a way to achieve this (see other answers) I would suggest that you shouldn't do that. Normal Android applications shouldn't need to know if the Home screen is about to display or not.
If you're trying to save data, put the data saving code in your onPause() method. If you're trying to give the user a way to change their mind about existing the application, you could intercept the key up/down for the Back key and the onBackPressed() method and present them with an "Are you sure?" prompt.
我已经为我的所有活动创建了一个基类,扩展了AppCompatActivity,并且它有一个静态计数器:
到目前为止,这已经足够好了;并且无论 API 版本如何。当然,它要求所有活动都扩展这个基类——在我的例子中,它们确实做到了。
编辑:我注意到在应用程序完全退出之前计数器降至零的一个实例,即方向发生更改且仅打开一个活动时。当方向改变时,该 Activity 被关闭并创建另一个 Activity,因此为最后一个 Activity 调用
onDestroyed
,然后在使用相同的 Activity 创建时调用onCreate
改变了方向。必须考虑到这种行为;可以使用 OrientationEventListener 。I've created a base class for all my activities, extending the AppCompatActivity, and which has a static counter:
This has worked well enough, so far; and regardless of API version. It requires of course that all activities extends this base class - which they do, in my case.
Edit: I've noticed one instance when the counter goes down to zero before the app completely exits, which is when the orientation is changed and only one activity is open. When the orientation changes, the activity is closed and another is created, so
onDestroyed
is called for the last activity, and thenonCreate
is called when the same activity is created with the changed orientation. This behaviour must be accounted for; OrientationEventListener could possibly be used.sandrstar 使用
ActivityManager
的解决方案的问题是:您需要权限才能以这种方式获取任务。我找到了一个更好的方法:
堆栈底部的
Activity
默认情况下应该始终获取此类别,而其他 Activity 不应获取它。但即使这在某些设备上失败,您也可以在启动
Activity
时进行设置:The Problem with sandrstar's solution using
ActivityManager
is: you need a permission to get the tasks this way.I found a better way:
The
Activity
on the Stack bottom should allways get this category by default while other Activities should not get it.But even if this fails on some devices you can set it while starting your
Activity
:Android 实现了 Activity 堆栈,我建议您阅读这里。看起来您想要做的就是检索调用活动:
getCallingActivity()
。如果当前活动是应用程序中的第一个活动,并且应用程序是从主屏幕启动的,那么它应该(我假设)返回null
。Android implements an Activity stack, I suggest you read about it here. It looks like all you want to do though is retrieve the calling activity:
getCallingActivity()
. If the current activity is the first activity in your application and the application was launched from the home screen it should (I assume) returnnull
.这里错过的一件事是“Home 键”点击,激活后,您无法从活动中检测到这一点,因此最好通过处理“后退键”按下并移动到所需的活动或以编程方式控制活动堆栈只是做必要的步骤。
此外,您无法确定是否可以通过将一些额外数据预设到打开活动的意图中来检测从“最近活动”列表中启动您的活动,因为在这种情况下会重用它。
The one thing that missed here, is the "Home key" click, when activated, you can't detect this from your activity, so it would better to control activity stack programmatically with handling "Back key" press and moving to required activity or just doing necessary steps.
In addition, you can't be sure, that starting your activity from "Recent Activity" list can be detected with presetting some extra data into intent for opening activity, as it being reused in that case.
**此代码的 kotlin 版本 **
**kotlin version of this code **
这里错过的一件事是“Home 键”点击,激活后,您无法从活动中检测到这一点,因此最好通过处理“后退键”按下并移动到所需的活动或以编程方式控制活动堆栈只是做必要的步骤。
此外,您无法确定是否可以通过将一些额外数据预设到打开活动的意图中来检测从“最近活动”列表中启动您的活动,因为在这种情况下会重用它。应该有错...
The one thing that missed here, is the "Home key" click, when activated, you can't detect this from your activity, so it would better to control activity stack programmatically with handling "Back key" press and moving to required activity or just doing necessary steps.
In addition, you can't be sure, that starting your activity from "Recent Activity" list can be detected with presetting some extra data into intent for opening activity, as it being reused in that case. there should be wrong...