Got exception: fragment already active

瘦欲@ 提交于 2019-12-02 15:22:42
Assaf Gamliel

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.

Warpzit

Try removing the previous fragment before adding the new one: https://stackoverflow.com/a/6266144/969325

remove() change fragment status to de-actiive. In your case, you just didn't call commit() after remove(..).

fragmentTransaction.remove(activeFragment);

You would do commit() after remove(), too.

fragmentTransaction.remove(activeFragment).commit();

Had the same issue. I was adding the fragment to backstack. And the error was because I didn't call popbackstack(). Using popbackstack helped me

I'm running into the same issue on Xamarin.android. Here's what the documentation says.

This can only be called before the fragment has been attached to its activity

Just call public method from fragment

if(userFragment==null){
                    userFragment = new UserFragment();
                    Bundle bundle = new Bundle();
                    bundle.putString(Constants.EXTRA_CUSTOMER, result);
                    userFragment.setArguments(bundle);
                }else{
                    try {
                        Customer customer = new Customer();
                        customer.parseCustomer(new JSONObject(result));
                        userFragment.updateVeiw(customer);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

First I start with describing why this happens and then I'll come up with the solution I found working... .

This issue happens when Android is removing the fragment from the stack but is not yet finished with removing. In order to check this, you can use the isRemoving() method of the fragment. If false, i.e. the fragment is not active, you can go on with setting the arguments using setArguments(bundle). Otherwise, you can't set arguments to an already active fragment and can only override it by addressing the same arguments using getArguments().putAll(bundle).

To summarize,

    if (myFrag.isRemoving()) {
        myFrag.getArguments().putAll(bundle);
    } else {
        myFrag.setArguments(bundle);
    }

If you want to avoid this, i.e. removing the fragment at once so there is no active fragment, you might want to use onBackPressed() in onBackStackChangedListener(), which will set the isRemoving() to false.

  1. Check whether your layout current one or old one for example

    setContentView(R.layout.activity_main);
    
  2. Delete old .gradle file in your project file and rebuild gradle file for project.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!