java.lang.IllegalStateException: Fragment already active

1. 問題描述:

在使用Fragment的setArguments(Bundle args)來傳遞參數到Fragment時,如果該Fragment是之前已經new出來的Fragment,

不是本次重新new出來的Fragment,這是再次調用setArguments(Bundle args) 則會發生該Exception。

2.解決方案:

Reading the setArguments(Bundle args) source will help you understand:

/**
* 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;
}

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 of and set the arguments again. Or you can usegetArguments() and then use the put methods of bundle to change it's values.



發佈了18 篇原創文章 · 獲贊 5 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章