I have a "main" fragment that contains tabs on the side, and when I click on any of those tabs, the ViewPager2 of the "main" fragment jumps to the specific fragment (each fragment is of a different class) in the FragmentStateAdapter.
Let's say that for one of those fragments, it has a button where if pressed, it replaces the current fragment in the FragmentStateAdapter, but the tabs are still at the side (we are still in the "main" fragment"). If I click on any of the tabs, I want it to work as before where it goes to the specific fragment.
I want to do this but I'm not sure how to do it or if it is the correct approach. Should I replace one of the fragments in the FragmentStateAdapter, or should I just destroy the ViewPager2 altogether and replace it with that desired fragment? Or is there a better approach?
This is how that FragmentStateAdapter looks like and I want to replace the EscortFragment when a button in it is clicked, with a different fragment.
public class HomeTabAdapter extends FragmentStateAdapter {
private ArrayList<Fragment> fragments;
private Context context;
public HomeTabAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle, Context context) {
super(fragmentManager, lifecycle);
this.context = context;
fragments = new ArrayList<>();
fragments.add(new HomeFragment());
fragments.add(new PurposeOfVisitFragment());
fragments.add(new GalleryFragment());
fragments.add(new EscortFragment());
fragments.add(new MapsFragment());
fragments.add(new FaqFragment());
fragments.add(new SupportFragment());
}
@NonNull
@Override
public Fragment createFragment(int position) {
return fragments.get(position);
}
@Override
public int getItemCount() {
return fragments.size();
}
}
Briefly, in your
HomeTabAdapterclass overridegetItemId()to generate a unique ID value for each of your fragments. Next time the base classFragmentStateAdapterneeds to access the fragment, it will see a different ID value for that position. This will cause it to callcreateFragment()again for that position.And if you override
getItemId(), you must also overridecontainsItem().So, for example, add the following to your
HomeTabAdapterclass:Notice that the method
replaceFragment()callsnotifyDataSetChanged(). This forces to check ID values immediately, rather than waiting until some later time.