Fix bug in main screen tabs state management

Tabs were not being destroyed/restored correctly due to a call to a
method that populated the view pager before it even had a chance of
restoring itself.

The solution was to null out the adapter before calling that method so
the view pager will postpone the populating process.
This commit is contained in:
Mauricio Colli 2020-03-08 09:09:04 -03:00
parent 9db2197be1
commit f2526ed5a8
No known key found for this signature in database
GPG Key ID: F200BFD6F29DDD85
1 changed files with 5 additions and 3 deletions

View File

@ -136,16 +136,17 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte
// Tabs // Tabs
//////////////////////////////////////////////////////////////////////////*/ //////////////////////////////////////////////////////////////////////////*/
public void setupTabs() { private void setupTabs() {
tabsList.clear(); tabsList.clear();
tabsList.addAll(tabsManager.getTabs()); tabsList.addAll(tabsManager.getTabs());
if (pagerAdapter == null || !pagerAdapter.sameTabs(tabsList)) { if (pagerAdapter == null || !pagerAdapter.sameTabs(tabsList)) {
pagerAdapter = new SelectedTabsPagerAdapter(requireContext(), getChildFragmentManager(), tabsList); pagerAdapter = new SelectedTabsPagerAdapter(requireContext(), getChildFragmentManager(), tabsList);
} }
// Clear previous tabs/fragments and set new adapter
viewPager.setAdapter(pagerAdapter); viewPager.setAdapter(null);
viewPager.setOffscreenPageLimit(tabsList.size()); viewPager.setOffscreenPageLimit(tabsList.size());
viewPager.setAdapter(pagerAdapter);
updateTabsIconAndDescription(); updateTabsIconAndDescription();
updateTitleForTab(viewPager.getCurrentItem()); updateTitleForTab(viewPager.getCurrentItem());
@ -194,6 +195,7 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte
this.internalTabsList = new ArrayList<>(tabsList); this.internalTabsList = new ArrayList<>(tabsList);
} }
@NonNull
@Override @Override
public Fragment getItem(int position) { public Fragment getItem(int position) {
final Tab tab = internalTabsList.get(position); final Tab tab = internalTabsList.get(position);