Got exception: fragment already active

前端 未结 8 1748
攒了一身酷
攒了一身酷 2021-01-30 12:49

I have a fragment;

MyFragment myFrag = new MyFragment();

I put bundle data to this fragment:

         


        
8条回答
  •  悲&欢浪女
    2021-01-30 13:21

    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 and set the arguments again. Or you can use getArguments() and then use the put method of the bundle to change its values.

提交回复
热议问题