@Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); tabController = new TabController(getChildFragmentManager(), R.id.container); if (savedInstanceState != null) { tabController.restore(savedInstanceState); } }
@Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); tabController.save(outState); }
private void hideVisibleFragment(FragmentTransaction fragmentTransaction) { final Fragment visibleFragment = getVisibleFragment(); if (visibleFragment != null) { showHideHandler.hide(fragmentTransaction, visibleFragment); } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = DataBindingUtil.setContentView(this, R.layout.activity_tab_controller); tabController = new TabController(getSupportFragmentManager(), R.id.container, new AttachDetachHandler()); tabController.setChangeListener(this); if (savedInstanceState != null) { binding.bottomBar.onRestoreInstanceState(savedInstanceState.getParcelable(BUNDLE_BOTTOM_BAR)); tabController.restore(savedInstanceState); } binding.bottomBar.setOnTabSelectListener(new BottomBarListener(this), true); }
inTransaction(transaction -> { if (!showHideHandler.isVisible(fragmentToShow)) { hideVisibleFragment(transaction); showFragment(fragmentToShow, transaction); hideVisibleFragment(transaction); Fragment addedFragment = addToFragmentManager(provider, transaction);
/** * Restore the state of the {@link TabController}. This will show the last visible fragment * before saving the state. * <br> * Make sure you call {@link #save(Bundle)} when saving your instance * state. * * @param savedInstanceState The saved instance state Bundle where the TabController state * wil be obtained from. */ public void restore(@Nullable Bundle savedInstanceState) { if (savedInstanceState != null) { final Bundle controllerState = savedInstanceState.getBundle(BUNDLE_KEY); if (controllerState == null) { throw new IllegalStateException("TabController's bundle not found in savedInstanceState. Did you call TabController::save in onSaveInstanceState(outState)?"); } final List<Fragment> fragments = getFMFragments(); inTransaction(transaction -> { for (Fragment fragment : fragments) { showHideHandler.restore(controllerState, transaction, fragment); } return new ArrayList<>(); }); } }
private void inTransaction(@NonNull TransactionBody body) { inTransaction(body, FragmentTransaction::commitNow); }
/** * Iterates through the fragments, returned by FragmentManager::getFragments. * * @return The first visible fragment found in the list or null. */ @Nullable public Fragment getVisibleFragment() { final List<Fragment> fragments = getFMFragments(); for (Fragment fragment : fragments) { if (showHideHandler.isVisible(fragment)) { return fragment; } } return null; }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = DataBindingUtil.setContentView(this, R.layout.activity_fragment_tab_controller); final TabControllerFragment tabControllerFragment = (TabControllerFragment) getSupportFragmentManager().findFragmentById(R.id.fragment); tabController = tabControllerFragment.getTabController(); tabController.setChangeListener(this); if (savedInstanceState != null) { binding.bottomBar.onRestoreInstanceState(savedInstanceState.getParcelable(BUNDLE_BOTTOM_BAR)); } binding.bottomBar.setOnTabSelectListener(new BottomBarListener(this), true); }
private void inAsyncTransaction(@NonNull TransactionBody body) { inTransaction(body, FragmentTransaction::commit); }
/** * Save the state of the {@link TabController} in order to be able to restore your last visible * fragment when your app restores. * <br> * Make sure you call {@link #restore(Bundle)} when restoring your instance state. * @param savedInstanceState The output Bundle where the state will be saved to. */ public void save(Bundle savedInstanceState) { final Bundle controllerState = new Bundle(); final List<Fragment> fragments = getFMFragments(); for (Fragment fragment : fragments) { showHideHandler.save(controllerState, fragment); } savedInstanceState.putBundle(BUNDLE_KEY, controllerState); }
@Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); tabController.save(outState); }
private void hide(FragmentProvider currentFragment) { final Fragment visibleFragment = fragmentManager.findFragmentByTag(currentFragment.getTag()); if (visibleFragment != null) { inTransaction(transaction -> { showHideHandler.hide(transaction, visibleFragment); return new ArrayList<>(); }); } }