By default, a Navigator does not support state saving, although each of the base implementations below do.
To support Navigator state saving, it can implement the
SavableNavigator interface.
In this method, the Navigator implementation must include everything necessary
to be able to reconstruct itself from a serialized state.
This includes the Navigator's own internal state, but also that of its
Scenes.
When using one of the base implementations this is done mostly for you, but you
can also choose to override the saveInstanceState
method to include your own
data.
1 class MyNavigator(
2 private val userId: String,
3 savedState: NavigatorState?
4 ) : SingleSceneNavigator(savedState) {
5
6 override fun saveInstanceState() : NavigatorState {
7 return super.saveInstanceState().also {
8 it["user_id"] = userId
9 }
10 }
11
12 companion object {
13
14 fun from(savedState: NavigatorState) : MyNavigator {
15 return MyNavigator(
16 userId = savedState["user_id"],
17 savedState = savedState
18 )
19 }
20 }
21 }
This snippet uses an existing SingleSceneNavigator class which
implements the SavableNavigator interface.
When the Navigator needs to have its state saved, the implementation hooks into
the saveInstanceState()
method and includes the userId
in the resulting state.
Similarly when restoring the Navigator, it uses a previously saved state to
retrieve the value of the userId
.