Cannot setMaxLifecycle for Fragment not attached to FragmentManager: ViewPager
Asked Answered
O

2

9

I have tried with so many given examples but nothing worked for me. When I am trying to remove a page dynamically from viewPager then I am getting exception:

Cannot setMaxLifecycle for Fragment not attached to FragmentManager

My PagerAdapter is given below:

public class MyPagerAdapter extends FragmentStatePagerAdapter {

    private final Context mContext;
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();
    long baseId = 0;

    public MyPagerAdapter(Context context, FragmentManager fm) {
        super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        mContext = context;
    }
    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

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

    public void addFragment(Fragment fragment, String title, int position) {
        mFragmentList.add(position, fragment);
        mFragmentTitleList.add(position, title);
    }

    public void removeFragment(int position) {
        mFragmentList.remove(position);
        mFragmentTitleList.remove(position);
    }

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

And to remove page I am calling method like

myPagerAdapter.removeFragment(viewPager.getCurrentItem());

I have tried with below given method too, but still getting exception

@Override
public int getItemPosition (Object object) {
    int index = mFragmentList.indexOf (object);
    if (index == -1)
        return POSITION_NONE;
    else
        return index;
}
Otherness answered 6/8, 2019 at 6:34 Comment(1)
You should probably just file a bug against Fragments - this wouldn't have anything to do with your code.Relaxation
H
1

Try Handler().post { myPagerAdapter.removeFragment(..) }

If you are running this method for some reason on another thread, make sure to put Handler(Looper.getMainLooper()).post { ... } instead.

The problem is that you might be doing a transaction while the ViewPager itself is handling an animation or its own state saving. Doing the above allows us to completely run the ViewPager's state saving completely before doing a transaction.

I had the same problem, doing this fixed it for me.

Specifically, this is my code that I am running inside of onPageScrollStateChanged(..) (presumably you are doing the same)

        override fun onPageScrollStateChanged(state: Int) {
            if (upcomingPage == 0 && state == ViewPager.SCROLL_STATE_IDLE) {
                homePagerRoot?.post { vpAdapter.clearExtraFragments() }
            }
        }
Hoxsie answered 21/8, 2019 at 23:45 Comment(0)
C
0

hope this is not too late the exception happens because you didn't access the right fragment manager attached to your fragment so you should use

    YourFragment.fragmentManager?.beginTransaction()!!.remove(YourFragment as Fragment).commitNowAllowingStateLoss()

sorry for kotlin try to convert back to java

Cibis answered 25/9, 2019 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.