FragmentPagerAdapter getItem is not being triggered

早过忘川 提交于 2019-11-28 18:35:48

Any workaround to overcome this problem?

I've downloaded your code and the problem appears because you don't handle those Fragments right. Most precisely you use nested Fragments in the ViewPager based Fragment and for that ViewPager you create the adapter like this:

MyFragmentPagerAdapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(this.getFragmentManager());

Instead, you should be using getChildFragmentManager() to bind the nested fragments:

MyFragmentPagerAdapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(this.getChildFragmentManager());

Also, you shouldn't pass data through a constructor to a Fragment as that data will not survive a configuration change and bad things will start to appear. Use a Bundle instead.

Global working tested solution.

getSupportFragmentManager() keeps the null reference some times and View pager does not create new fragment instance.Since it finds reference to same fragment. So to over come this use getChildFragmentManager() solves problem in simple way.

Don't

new PagerAdapter(getSupportFragmentManager(), fragments);

Do

new PagerAdapter(getChildFragmentManager() , fragments);

In my case I was correctly calling

MyFragmentPagerAdapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(this.getChildFragmentManager());

but then in the nested fragment I was trying to replace the container fragment with another one by using:

getFragmentManager()

You need to go to the activity and call

getActivity().getSupportFragmentManager();

Simple use FragmentStatePagerAdapter instead of FragmentPagerAdapter

or

you can use new MyFragmentPagerAdapter(this.getChildFragmentManager())

Hope it will help you :)

In my cases it worked after add this to my FragmentPagerAdapter:

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

and I also used getChildFragmentManager() like Luksprog said

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