Android Intent.FLAG_ACTIVITY_SINGLE_TOP和Intent.FLAG_ACTIVITY_CLEAR_TOP

时间:2021-03-25 15:34:47

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?

使用这种方法有任何陷阱吗?

2 个解决方案

#1


13  

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开发人员文档。

#2


1  

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-D and You have launched another intent Starting Activity A with FLAG_ACTIVITY_CLEAR_TOP at 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).
  • 假设您有BackStack的ABCD并且您已经启动了另一个意图使用FLAG_ACTIVITY_CLEAR_TOP启动活动A此时Android将要做的是简单清除活动A顶部的所有活动,意味着现在您的BackStack看起来像 - > A(是就是这样,因为你已经清除了它上面的所有活动)。

  • 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)
  • 并且在第二个场景中假设您有相同的BackStack A-B-C-D并且您使用FLAG_ACTIVITY_SINGLE_TOP启动了Intent Starting Activity A,现在您的BackStack看起来像 - > A-B-C-D-A(困惑?不要担心下一个示例将清除所有您的混淆)

  • Third Scenario, We start with the same BackStack A-B-C-D and We will launch an Intent Starting Activity D with 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.

    第三种情况,我们从相同的BackStack ABCD开始,我们将使用FLAG_ACTIVITY_SINGLE_TOP启动Intent Starting Activity D,现在我们的BackStack看起来像 - > ABCD(是的,BackStack保持相同的顺序,因为我们的FLAG禁止启动Same Activity如果它是当前在堆栈顶部并在用户屏幕上显示。

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

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

Got it? FLAG_ACTIVITY_SINGLE_TOP is 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仅在您启动当前正在屏幕上显示的相同活动并且您要禁止启动现有活动的新实例时才有用。

#1


13  

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开发人员文档。

#2


1  

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-D and You have launched another intent Starting Activity A with FLAG_ACTIVITY_CLEAR_TOP at 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).
  • 假设您有BackStack的ABCD并且您已经启动了另一个意图使用FLAG_ACTIVITY_CLEAR_TOP启动活动A此时Android将要做的是简单清除活动A顶部的所有活动,意味着现在您的BackStack看起来像 - > A(是就是这样,因为你已经清除了它上面的所有活动)。

  • 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)
  • 并且在第二个场景中假设您有相同的BackStack A-B-C-D并且您使用FLAG_ACTIVITY_SINGLE_TOP启动了Intent Starting Activity A,现在您的BackStack看起来像 - > A-B-C-D-A(困惑?不要担心下一个示例将清除所有您的混淆)

  • Third Scenario, We start with the same BackStack A-B-C-D and We will launch an Intent Starting Activity D with 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.

    第三种情况,我们从相同的BackStack ABCD开始,我们将使用FLAG_ACTIVITY_SINGLE_TOP启动Intent Starting Activity D,现在我们的BackStack看起来像 - > ABCD(是的,BackStack保持相同的顺序,因为我们的FLAG禁止启动Same Activity如果它是当前在堆栈顶部并在用户屏幕上显示。

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

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

Got it? FLAG_ACTIVITY_SINGLE_TOP is 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仅在您启动当前正在屏幕上显示的相同活动并且您要禁止启动现有活动的新实例时才有用。