|
| 1 | +# Managing Screen State |
| 2 | + |
| 3 | +<title instance="decompose-router">Managing Screen State</title> |
| 4 | +**State** (or UiState) model usually capture everything on a given screen that can change over time. This state is usually contained by a **State Holder** (e.g:- a `Component`, `ViewModel`, `Presenter` or whatever) and mutate that state over time by applying business logic |
| 5 | + |
| 6 | +## Scoping a State Holder to a Router |
| 7 | + |
| 8 | +If you want your screen level state holder to be scoped to a given screen, use `rememberOnRoute` |
| 9 | +1. This makes your instances survive configuration changes (on Android) |
| 10 | +2. Holds-on the instance as long as it is in the backstack |
| 11 | + |
| 12 | +```kotlin |
| 13 | +class List |
| 14 | + |
| 15 | +@Composable |
| 16 | +fun ListScreen() { |
| 17 | + val list: List = rememberOnRoute(List::class) { _ -> List() } |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +If you want this instance to be recomputed, you can also provide a key to it |
| 22 | +```kotlin |
| 23 | +class Details(val id: String) |
| 24 | + |
| 25 | +@Composable |
| 26 | +fun DetailsScreen(id: String) { |
| 27 | + val details: Details = rememberOnRoute(Details::class, key = id) { _ -> Details(id) } |
| 28 | +} |
| 29 | +``` |
| 30 | +> Due to this [issue](https://github.com/JetBrains/compose-multiplatform/issues/2900), you will still need to provide this |
| 31 | +> type `ListScreen:class` manually for now. |
| 32 | +> Once resolved, you will be able to use the `inline` `refied` (and nicer) signature |
| 33 | +> ```kotlin |
| 34 | +> val list: List = rememberOnRoute { _ -> List() } |
| 35 | +> ``` |
| 36 | +{style="warning"} |
| 37 | +
|
| 38 | +### Integrating with Decompose Components |
| 39 | +
|
| 40 | +Integrating Decompose components works the same way. `RouterContext` can provide you a `ComponentContext` if needed be |
| 41 | +
|
| 42 | +```kotlin |
| 43 | +import com.arkivanov.decompose.ComponentContext |
| 44 | +
|
| 45 | +class ListComponent(context: RouterContext): ComponentContext by context |
| 46 | +
|
| 47 | +@Composable |
| 48 | +fun MyScreen() { |
| 49 | + val listComponent: ListComponent = rememberOnRoute(ListComponent::class) { context -> |
| 50 | + ListComponent(context) |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | +{collapsible="true" collapsed-title="class ListComponent(context: RouterContext): com.arkivanov.decompose.ComponentContext by context"} |
| 55 | + |
| 56 | +### Integrating with Android Architecture Component ViewModels |
| 57 | + |
| 58 | +Integrating [AAC `ViewModel`](https://developer.android.com/topic/libraries/architecture/viewmodel)s works the same way. You can scope `ViewModel`s directly and these will be stay on the stack/page/slot as long it is needed |
| 59 | + |
| 60 | +```kotlin |
| 61 | +import androidx.lifecycle.ViewModel |
| 62 | + |
| 63 | +class ListViewModel: ViewModel() |
| 64 | + |
| 65 | +@Composable |
| 66 | +fun ListScreen() { |
| 67 | + val viewModel: ListViewModel = rememberOnRoute(type = ListViewModel::class) { ListViewModel() } |
| 68 | +} |
| 69 | +``` |
| 70 | +{collapsible="true" collapsed-title="class ListViewModel: androidx.lifecycle.ViewModel()"} |
| 71 | + |
| 72 | +## Restoring Initial State after System-initiated Process Death |
| 73 | + |
| 74 | +> [System-initiated process death](https://developer.android.com/topic/libraries/architecture/saving-states#onsaveinstancestate) may kill your android process when the system is running out of memory. |
| 75 | +> |
| 76 | +> Router will restore the screens, backstack and any savable states in them (like scroll position) automatically |
| 77 | +> |
| 78 | +> However, you will still need to save and restore state withing your state holders |
| 79 | +
|
| 80 | + |
| 81 | +**Initial state** is the first-ever state your screen is rendered with. This is usually the **default state** for your screen. |
| 82 | +However, if your app is being restored after system initiated process death, we want to _derive_ the initial state |
| 83 | +from **saved state** of the previous process (instead of the default) |
| 84 | + |
| 85 | +Within your **State Holder**, you can derive the initial state by using `RouterContext.state`. |
| 86 | +Make sure to point the supplier lambda back to your state flow so that it knows where to grab the latest state from to save |
| 87 | + |
| 88 | +```kotlin |
| 89 | +class List(context: RouterContext) { |
| 90 | + private val initialState: ListState = context.state(ListState()) { states.value } |
| 91 | + private val _state: MutableStateFlow<ListState> = MutableStateFlow(initialState) |
| 92 | + val states: StateFlow<ListState> = _state |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +### Integrating with Molecule |
| 97 | + |
| 98 | +For [Molecule](https://github.com/cashapp/molecule), initial state can be provided to your `moleculeFlow` in conjunction with `stateIn` |
| 99 | + |
| 100 | +```kotlin |
| 101 | +class List(context: RouterContext) { |
| 102 | + private val initialState: ListState = context.state(ListState()) { states.value } |
| 103 | + val states: StateFlow<EquipmentSelectionState> = moleculeFlow(ContextClock) { ListPresenter() } |
| 104 | + .stateIn(this, SharingStarted.Lazily, initialState) |
| 105 | +} |
| 106 | +``` |
| 107 | +{collapsible="true" collapsed-title="moleculeFlow(ContextClock) { ListPresenter(initialState) }.stateIn(this, SharingStarted.Lazily, initialState)"} |
0 commit comments