有例外:碎片已激活 [英] Got exception: fragment already active

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

问题描述

我有一个片段

MyFragment myFrag = new MyFragment();

我把捆绑数据以这个片断:

Bundle bundle = new Bundle(); 
bundle.putString("TEST", "test");
myFrag.setArguments(bundle);

然后,我的替换旧片段,这一个和穿上backstack

Then, I replace old fragment with this one and put on backstack:

//replace old fragment
fragmentTransaction.replace(R.id.fragment_placeholder, myFrag, "MyTag");
//put on backstack
fragmentTransaction.addToBackStack(null);
//commit & get transaction ID
int transId = fragmentTransaction.commit();

后来,我的弹出backstack 上述事务ID(<$ C C $> TRANSID ):

Later, I pop backstack with the above transaction ID(transId):

//pop the transaction from backstack
fragmentManager.popBackStack(transId,FragmentManager.POP_BACK_STACK_INCLUSIVE); 

后来,我将捆绑数据作为参数再次到我的片段( myFrag ):

Later, I set bundle data as argument again to my fragment(myFrag):

//Got Java.lang.IllegalStateException: fragment already active
myFrag.setArguments(bundle);

正如你看到的,我上面的code有例外 Java.lang.IllegalStateException:碎片已激活我不明白为什么 myFrag 依然活跃,虽然我已经出现了它的交易从backstack。,总之,因为我得到的例外,我想我没有选择,只能去积极的片段,所以,我所做的:

As you see, my above code got exception Java.lang.IllegalStateException: fragment already active . I don't understand why myFrag is still active though I have popped the transaction of it from backstack., anyhow, since I got the exception I thought I have no choice but de-active the fragment, So, I did:

Fragment activeFragment = fragMgr.findFragmentByTag("MyTag");
fragmentTransaction.remove(activeFragment);

我不知道如果我的上述code真的能去主动的片段,因为我没有找到如何去激活一个片段。 :(

I am not sure if my above code really can de-active the fragment, since I didn't find how to de-active an fragment. :(

在这之后,当我试图捆绑数据 myFrag 重新设置为我的片段,我还是得到了同样的错误:

After that, when I try to set bundle data to my fragment myFrag again, I still got the same error:

Java.lang.IllegalStateException: fragment already active

似乎连我删除片段,但仍活跃... 为什么?如何去激活一个片段?

推荐答案

读<一href="http://developer.android.com/reference/android/app/Fragment.html#setArguments%28android.os.Bundle%29">setArguments(Bundle参数)源将帮助您了解:

/**
* Supply the construction arguments for this fragment.  This can only
* be called before the fragment has been attached to its activity; that
* is, you should call it immediately after constructing the fragment.  The
* arguments supplied here will be retained across fragment destroy and
* creation.
*/
public void setArguments(Bundle args) {

    if (mIndex >= 0) {
        throw new IllegalStateException("Fragment already active");
    }
    mArguments = args;
}

您不能使用<一个href="http://developer.android.com/reference/android/app/Fragment.html#setArguments%28android.os.Bundle%29">setArguments(Bundle参数)又在同一片段与code。你想要做我猜要​​么是创建一个 片段并重新设置参数。或者你可以使用<一个href="http://developer.android.com/reference/android/app/Fragment.html#getArguments%28%29">getArguments()然后用包的方法来改变自己的价值观。

You cannot use setArguments(Bundle args) again in your code on the same Fragment. What you want to do I guess is either create a new Fragment and set the arguments again. Or you can use getArguments() and then use the put method of the bundle to change its values.

我希望它帮助。

这篇关于有例外:碎片已激活的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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