Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear view pager fragments

I have a fragment A, in it I have a view pager with two fragment 1.1 and fragment 1.2 these fragments are children fragments of 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 by the options inside fragment B. The problem is I can`t update the fragments as they are already inside childfragment manager, also when I return back to fragment A getItem(int position) of the view pager not called. I managed to remove all the child fragments before I am setting the view pager adapter but I am not sure this it the right way to accomplish this functionality.

I have 1.1 and 1.2 references inside Fragment A

Thanks.

like image 323
visionix visionix Avatar asked Dec 11 '22 22:12

visionix visionix


2 Answers

Had the same problem. For me it worked to override FragmentStatePagerAdapter

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

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

}

This work for me.

like image 108
saravanan Avatar answered Jan 13 '23 14:01

saravanan


A simple way (not the best way) to do this is to override getItemPosition() in your PagerAdapter:

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

When the adapter's notifyDataSetChanged() method is called, the ViewPager will call getItemPosition() on the views it already knows about. By returning POSITION_NONE, you are telling the ViewPager to remove the view. Once the ViewPager removes the view, it will call getItem() to get an updated view.

So when the user returns back to Fragment A you would call:

viewPager.getAdapter().notifyDataSetChanged();

This will start the process of updating all your views.

like image 34
kris larson Avatar answered Jan 13 '23 13:01

kris larson