Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

got "java.lang.IllegalStateException: Fragment already active" when layout is not empty [duplicate]

I have activity and pager with 3 fragments. I use tabs for switching. When I click some button I want to replace fragment0 with new one. It works only when fragment0 has empty layout. If I set some content in layout of fragment0 (there has to be listview) exception is thrown:

java.lang.IllegalStateException: Fragment already active
    at android.support.v4.app.Fragment.setInitialSavedState(Fragment.java:503)
    at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:110)
    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:649)

I use following layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:id="@+id/all_exp_frag"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">

<ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/all_expenses"/>

</LinearLayout>

Activity:

public class Activity extends FragmentActivity {

    private List<Fragment> fragments;
    private MyPagerAdapter pagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        fragments = asList(new Fragment0(), new Fragment1(), new Fragment2());
        // initialize pager, etc.
        pagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), pager);
    }

    // handle add new fragment button
    @Override
    public boolean onOptionsItemSelected(MenuItem item) { 
        pagerAdapter.replaceCurrentFragment(new Fragment3());
    }

    public class MyPagerAdapter extends FragmentStatePagerAdapter {

        final ViewPager pager;

        public MyPagerAdapter(FragmentManager fm, ViewPager pager) {
            super(fm);
            this.pager = pager;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return fragments.get(position).getFragmentTitle();
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getItemPosition(Object object) {
            if (object instanceof ExpensesFragment) {
                return POSITION_NONE;
            }
            return POSITION_UNCHANGED;
        }

        public void replaceCurrentFragment(BaseFragment fragment) {
            final int currentPosition = pager.getCurrentItem();
            final BaseFragment old_fragment = fragments.get(currentPosition);
            if (old_fragment == null) {
                return;
            }
            this.startUpdate(pager);
            getSupportFragmentManager()
                    .beginTransaction()
                    .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                    .remove(old_fragment)
                    .add(pager.getId(), fragment)
                    .commitAllowingStateLoss();
            fragments.set(currentPosition, fragment);
            this.finishUpdate(pager);
            this.notifyDataSetChanged(); //IN THIS LINE EXCEPTION IS THROWN WHEN LAYOUT IS NOT EMPTY (contains listview). IF EMPTY - IT WORKS FINE.
            listener.onPageSelected(0);
        }

    }
}

Application throws exception on line

this.notifyDataSetChanged();

if layout is not empty.

I cannot find solution how to avoid this exception and replace fragment correctly.

EDITED

https://android.googlesource.com/platform/frameworks/support/+/jb-mr1-dev/v4/java/android/support/v4/app/Fragment.java

as I checked Fragment class contains following method:

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

maybe the way I'm adding fragments is not correct..

like image 837
vetalitet Avatar asked Nov 11 '22 12:11

vetalitet


1 Answers

Check whether the fragment is already added or not using the Fragment class' #isAdded() method. Then replace or add the fragment accordingly:

Let's assume your fragment was called fragment

// Fragment object
Fragment fragment;

// Check to see if it's added or not like this
if (!fragment.isAdded()) {
    // Things you could do here:
    // -- Set argument
    // -- Begin a transaction 
    // -- etc...  
}
like image 183
Jiju Induchoodan Avatar answered Nov 14 '22 21:11

Jiju Induchoodan