简体   繁体   中英

ViewPager state persistent in fragment

I have a scenario where I am using bottomNavigation with 3 fragments to load in single Activity. In fragment two I have a pager with multiple fragments. I wanted to retain the state of fragment two once all respective fragments loaded with views in pager.

After hectic things I think I have resolved issue.. I have used Singleton design pattern to handle this,

My parent Fragment is a singleton class which will hold fragment list that list will have a fragment to render UI.

Once user will click on bottomNavigation it will load same fragment instance as it already has a list of fragments so once I will insure that it has list I am rendering pager with same list and its same instance of fragment.

Code: To persiste

if(listOfFragments!=null&&listOfFragments.size>0)
    binding.tabLayout.setupWithViewPager(notificationBinding.notificationTabsContainer);
    binding.tabLayout.setOnTabSelectedListener(this);
    binding.notificationTabsContainer.setAdapter(pagerAdapter);
    binding.notificationTabsContainer.setOffscreenPageLimit(listOfFragments.size());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM