resetRoot

abstract fun resetRoot(newRoot: Screen, saveState: Boolean = false, restoreState: Boolean = false): ImmutableList<Screen>(source)

Clear the existing backstack of screens and navigate to newRoot.

This is useful in preventing the user from returning to a completed workflow, such as a tutorial, wizard, or authentication flow.

Example

val navigator = Navigator()
navigator.push(LoginScreen1)
navigator.push(LoginScreen2)

// Login flow is complete. Wipe backstack and set new root screen
val loginScreens = navigator.resetRoot(HomeScreen)

Multiple back stacks

The saveState and restoreState parameters enable functionality what is commonly called 'multiple back stacks'. By optionally saving, and later restoring the back stack, you can enable different root screens to have their own back stacks. A common use case is with the bottom navigation bar UX pattern.

navigator.resetRoot(HomeNavTab1, saveState = true, restoreState = true)
// User navigates to a details screen
navigator.push(EntityDetails(id = foo))
// Later, user clicks on a bottom navigation item
navigator.resetRoot(HomeNavTab2, saveState = true, restoreState = true)
// Later, user switches back to the first navigation item
navigator.resetRoot(HomeNavTab1, saveState = true, restoreState = true)
// The existing back stack is restored, and EntityDetails(id = foo) will be top of
// the back stack

There are times when saving and restoring the back stack may not be appropriate, so use this feature only when it makes sense. A common example where it probably does not make sense is launching screens which define a UX flow which has a defined completion, such as onboarding.

Parameters

newRoot

The new root Screen

saveState

Whether to save the current entry list. It can be restored by passing the current root Screen to resetRoot with restoreState = true

restoreState

Whether any previously saved state for the given newRoot should be restored. If this is false or there is no previous state, the back stack will only contain newRoot.