diff --git a/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationController.kt b/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationController.kt index 427a58b..b8a1bc6 100644 --- a/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationController.kt +++ b/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationController.kt @@ -19,7 +19,7 @@ import ru.touchin.roboswag.core.utils.ShouldNotHappenException class BottomNavigationController( private val context: Context, private val fragmentManager: FragmentManager, - private val viewControllers: SparseArray>, Parcelable>>, + private val viewControllers: SparseArray, @IdRes private val contentContainerViewId: Int, @LayoutRes private val contentContainerLayoutId: Int, private val wrapWithNavigationContainer: Boolean = false, @@ -63,7 +63,7 @@ class BottomNavigationController( fun navigateTo(@IdRes itemId: Int, state: Parcelable? = null) { // Find view controller class that needs to open - val (viewControllerClass, defaultViewControllerState) = viewControllers[itemId] ?: return + val (viewControllerClass, defaultViewControllerState, saveStateOnSwitching) = viewControllers[itemId] ?: return if (state != null && state::class != defaultViewControllerState::class) { throw ShouldNotHappenException( "Incorrect state type for navigation tab root ViewController. Should be ${defaultViewControllerState::class}" @@ -76,7 +76,7 @@ class BottomNavigationController( val viewControllerName = viewControllerClass.canonicalName var fragment = fragmentManager.findFragmentByTag(viewControllerName) - if (state == null && fragment != null) { + if (saveStateOnSwitching && state == null && fragment != null) { transaction.attach(fragment) } else { // If fragment already exist remove it first diff --git a/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationFragment.kt b/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationFragment.kt index fc659a9..d514716 100644 --- a/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationFragment.kt +++ b/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationFragment.kt @@ -29,7 +29,7 @@ abstract class BottomNavigationFragment : Fragment() { protected abstract val wrapWithNavigationContainer: Boolean - protected abstract val navigationViewControllers: SparseArray>, Parcelable>> + protected abstract val navigationViewControllers: SparseArray protected open val reselectListener: (() -> Unit) = { getNavigationActivity().innerNavigation.up(inclusive = true) } @@ -72,4 +72,14 @@ abstract class BottomNavigationFragment : Fragment() { private fun getNavigationActivity() = requireActivity() as BottomNavigationActivity + data class TabData( + val viewControllerClass: Class>, + val viewControllerState: Parcelable, + /** + * It can be useful in some cases when it is necessary to create ViewController + * with initial state every time when tab opens. + */ + val saveStateOnSwitching: Boolean = true + ) + }