Search code examples
androidandroid-fragmentsandroid-viewpagerandroid-tablayoutnavigation-drawer

TabLayout keeps old adapter's fragments


I'm trying to change a TabLayout's content after a button press.

I have five fragments. At start the TabLayout contains 1. 2. and 3. fragment, when I press a navigation drawer button I'd like to change the Tablayout so that it would contain only 3. 4.

Currently I have no idea how I could remove all the fragments at once from the TabLayout and replace them as the adapter tells it should be.

I tried replacing the adapter (it does not contain any mention of fragments 1, 2 and 3), setting the adapter to null, calling .notifyDataSetChanged(); in various combinations. But nothing I have come up with forces the TabLayout to actually update itself and get rid of the old fragments and replace them with the new ones.

I've only managed to update the first fragment in the tablayout with this, then the proper fragment is shown (what the adapter actually tells):

                Fragment fragment = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":" + viewPager.getCurrentItem());
                if (fragment != null) // could be null if not instantiated yet
                {
                    if (fragment.getView() != null) {
                        // Pop the backstack on the ChildManager if there is any. If not, close this activity as normal.
                        fragment.getActivity().getSupportFragmentManager().beginTransaction().remove(fragment).commit();

                    }
                }

How could I do what I wish to do?


Solution

  • The issue seems resolved, I just updated everything to the latest version. I hope noone else encounters this issue, ever.