How to clear pager fragments

I have fragment A

, in it I have a pager with two fragment 1.1

and fragment 1.2

these fragments are fragments of children fragment A

.

Now when the user gets to fragment B and then back to A, I need to update the child fragments 1.1 and 1.2 with the options inside fragment B. The problem is that I cannot update the fragments as they are already in the childfragment manager, also when I go back to snippet A of the getItem(int position)

non-callable view pager. I was able to remove all child fragments before I installed the pager adapter, but I'm not sure if this is the correct way to accomplish this functionality.

I have 1,1 and 1,2 links inside fragment A

Thank.

+3


source to share


3 answers


There was the same problem. It worked for me to override FragmentStatePagerAdapter

@Override
public Parcelable saveState() {
    return null;
}

@Override
public void restoreState(Parcelable state, ClassLoader loader) {

}

      



This job is for me.

+5


source


The easy (not the best) way to do this is to override getItemPosition()

in PagerAdapter

:

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

      

When the adapter method is called notifyDataSetChanged()

, it ViewPager

will call getItemPosition()

on views it already knows about. By returning POSITION_NONE you tell it to ViewPager

remove the view. Once it ViewPager

removes the view, it will call getItem()

to get the updated view.



So, when the user returns to Fragment A, you call:

viewPager.getAdapter().notifyDataSetChanged();

      

This will start the process of updating all of your views.

+4


source


I spent two days struggling with this and I just found a solution to my problem:

Just use FragmentStatePagerAdapter instead of FragmentPagerAdapter and it will work like a charm !!!

+2


source







All Articles