/** * 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; }
/** * 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); }
/** * 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<>(); }); } }