FLAG_ACTIVITY_CLEAR_TOP? [英] FLAG_ACTIVITY_CLEAR_TOP?

查看:212
本文介绍了FLAG_ACTIVITY_CLEAR_TOP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,为什么 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)将无法正常工作?

我有3个活动,让我们说A,B和C。

当我试图发起一个活动从C与code:

 意向书我=新的意图(这一点,的A.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(ⅰ);
 

这只是开始活动A,但不清除顶! -_-

我也尝试过使用 setFlags()

我读到不同的问题上因此,关于这个问题,但我无法找到正确的答案。 > _<

有人请帮助!

修改

$ C $下onBack pressed()的活动A所要求的@ codeMagic。

  @覆盖
公共无效onBack pressed(){
    如果(wvLogin.canGoBack())
        wvLogin.goBack();
    其他
        super.onBack pressed();
}
 

解决方案

从为<一个文档href="http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP">FLAG_ACTIVITY_CLEAR_TOP:

  

如果设置和活动正在开展已经在运行   当前的任务,然后,而不是推出的一个新实例   活性,所有的在上面的其他活动的将被关闭,并   这个意图将被传递到(现在的顶部)老年活动作为   新的意图。

如您在您的评论加入,活动A已经完成调用在B之前,因此这种情况不适用。活动A的新实例将推出替代。

在我看来,你有两个选择:

1)使用 Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK 标志。这将启动一个活动的堆栈的根。它的工作原理,但栈中的任何其他活动都将丢失。假设A的第一个活动(或者至少,你不感兴趣的任务堆栈中的任何previous活动),那么也没关系。 注:CLEAR_TASK需要API级别11

2)的另一种可能的解决方案的情况下(previous假设不是真)将是不使用意图标志在所有的

  • B开始下与 startActivityForResult()
  • 而不是调用A的,C结束,已经设置的结果对B,表明一个必须被启动。
  • B.afterActivityResult(),完成B和推出。

As the title says, Why intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) or intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) won't work?

I have 3 Activities let us say A, B and C.

When I am trying to launch Activity A from C with code:

Intent i = new Intent(this, A.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);

It simply starts Activity A but doesn't clear the top.! -_-

I have also tried using setFlags().

I read different questions on SO about this problem, but I couldn't find the right answer. >_<

Somebody please help!

Edit

Code for onBackPressed() in activity 'A' as requested by @codeMagic.

@Override
public void onBackPressed(){
    if(wvLogin.canGoBack())
        wvLogin.goBack();
    else
        super.onBackPressed();
}

解决方案

From the documentation for FLAG_ACTIVITY_CLEAR_TOP:

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

As you added in your comment, the activity A has been finished before calling B, so this situation doesn't apply. A new instance of activity A will be launched instead.

As I see it, you have two options here:

1) Use the Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK flags. This will start activity A as the root of the stack. It works, but any other activities in the stack will be lost. Assuming A was the first activity (or at least, that you are not interested in any previous activities in the task stack) then it doesn't matter. Note: CLEAR_TASK requires API Level 11.

2) Another possible solution (in case the previous assumption is not true) would be to not use intent flags at all:

  • B starts C with startActivityForResult().
  • Instead of calling A, C finishes, having set a result for B indicating that A must be launched.
  • In B.afterActivityResult(), finish B and launch A.

这篇关于FLAG_ACTIVITY_CLEAR_TOP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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