Android Intent.FLAG_ACTIVITY_SINGLE_TOP 和 Intent.FLAG_ACTIVITY_CLEAR_TOP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3217942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:11:33  来源:igfitidea点击:

Android Intent.FLAG_ACTIVITY_SINGLE_TOP AND Intent.FLAG_ACTIVITY_CLEAR_TOP

androidandroid-intent

提问by shaneburgess

I have an app that I have running a media player and I want to resume the activity from my apps home activity.

我有一个运行媒体播放器的应用程序,我想从我的应用程序主页活动中恢复活动。

I can successfully do this by adding the following flags to the startActivity Call:

我可以通过将以下标志添加到 startActivity 调用来成功执行此操作:

myIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

I am worried that this is not an ideal way to do things since it took me a long time to find it. It made me think that no one uses it very much.

我担心这不是一种理想的做事方式,因为我花了很长时间才找到它。这让我觉得没有人经常使用它。

Are there any pitfalls to using this method?

使用这种方法有什么陷阱吗?

回答by AgileYogi

Just to make sure I understand your question correctly: Say your activity stack is something like A -> B -> C, and now from C you want to clear the stack and get back to A.

只是为了确保我正确理解您的问题:假设您的活动堆栈类似于 A -> B -> C,现在您想从 C 清除堆栈并返回 A。

If this is what you want, then those intent flags are correct and should work as expected, as per the Android-developer docs.

如果这是您想要的,那么根据Android-developer docs,这些意图标志是正确的并且应该按预期工作。

回答by Shubham Soni

I know that this question is quite older and may you have solved your problem and may be travelled to mars and back in those years.But just to make things clear for the people coming here and looking for an explanation:

我知道这个问题已经很老了,可能你已经解决了你的问题,并且可能在那些年里去过火星和回来。但只是为了让来这里寻找解释的人清楚:

According to Official Documentation:

根据官方文档:

If set, the activity will not be launched if it is already running at the top of the history stack.

如果设置,如果活动已经在历史堆栈的顶部运行,则不会启动。

  • Suppose you have BackStack of A-B-C-Dand You have launched another intent Starting Activity Awith FLAG_ACTIVITY_CLEAR_TOPat this point what android will be do is Simply Clear All the Activities on the Top of Activity A, means now your BackStack Will Look like-> A(yes that's it because you have cleared all the activities on top of it).
  • And In Second Scenario Suppose You have the same BackStack A-B-C-D and you launched an Intent Starting Activity A with FLAG_ACTIVITY_SINGLE_TOP, now your BackStack will look like-> A-B-C-D-A (Confused? Don't Worry Next Example Will Clear All Your Confusions)
  • Third Scenario, We start with the same BackStack A-B-C-Dand We will launch an Intent Starting Activity Dwith FLAG_ACTIVITY_SINGLE_TOP, now our BackStack will look like-> A-B-C-D(Yes the BackStack remains in the Same Order because our FLAG prohibits the launch Same Activity If it is the Currently on Top of the Stack and Showing on the User screen.

  • Last Scenario, We start with our same BackStack A-B-C-Dand We will launch an Intent Starting Activity Dbut this time with no FLAGS, now our BackStack will look like-> A-B-C-D-D.

  • 假设你有 BackStackA-B-C-D并且你已经启动了另一个意图从 此时开始Activity AFLAG_ACTIVITY_CLEAR_TOPandroid 将要做的是简单地清除顶部的所有活动,这Activity A意味着现在你的 BackStack 看起来像 -> A(是的,就是这样,因为你已经清除了所有上面的活动)。
  • 在第二个场景中假设你有相同的 BackStack ABCD 并且你启动了一个带有 FLAG_ACTIVITY_SINGLE_TOP 的 Intent 启动活动 A,现在你的 BackStack 看起来像 -> ABCDA(困惑?别担心下一个例子会清除你所有的困惑)
  • 第三种情况,我们使用相同的堆栈中开始A-B-C-D,我们将推出一个Intent开始Activity DFLAG_ACTIVITY_SINGLE_TOP,现在我们的堆栈中看起来喜欢- > A-B-C-D如果这是目前在最前(是返回堆栈仍然以相同的顺序,因为我们的国旗禁止启动同一活动堆栈和显示在用户屏幕上。

  • 最后一个场景,我们从相同的 BackStack 开始,A-B-C-D我们将启动一个 Intent 启动,Activity D但这次没有 FLAGS,现在我们的 BackStack 看起来像 -> A-B-C-D-D

Got it? FLAG_ACTIVITY_SINGLE_TOPis only useful When you are starting the Same Activity Which is Currently Showing On the Screen and You want to prohibit the launch new Instance of the Existing Activity.

知道了?FLAG_ACTIVITY_SINGLE_TOP仅当您启动当前显示在屏幕上的同一活动并且您想禁止启动现有活动的新实例时才有用。