Daniel Okanin
01/09/2025, 4:54 PMPablichjenkov
01/09/2025, 5:20 PMDaniel Okanin
01/09/2025, 5:22 PMPablichjenkov
01/09/2025, 5:22 PMDaniel Okanin
01/09/2025, 5:23 PMPablichjenkov
01/09/2025, 5:25 PMPablichjenkov
01/09/2025, 5:26 PMDaniel Okanin
01/09/2025, 5:27 PMPablichjenkov
01/09/2025, 5:28 PMclass ComponentStateController {
uiState: StateFlow<ComponentUiState>
fun start() {}
fun stop() {}
}
sealed class
ComponentUiState { ... }
Daniel Okanin
01/09/2025, 5:29 PMPablichjenkov
01/09/2025, 5:29 PMDaniel Okanin
01/09/2025, 5:30 PMPablichjenkov
01/09/2025, 5:31 PMCasey Brooks
01/09/2025, 5:53 PMrememberCoroutineScope()
and consider them ephemeral. Any data I would like persisted across lifecycle changes, I push back into the Data layer, so VMs are only active when their component is visible on the screen and can be created/destroyed as needed without risk of data loss.Daniel Okanin
01/09/2025, 5:58 PMDaniel Okanin
01/09/2025, 6:02 PMPablichjenkov
01/09/2025, 7:08 PMCasey Brooks
01/09/2025, 7:10 PMfun Navigation() {
NavHost(...) {
composable<AppScreen.Screen1> { Screen1() }
composable<AppScreen.Screen2> { Screen2() }
}
}
class Screen1ViewModel(val viewModelScope: CoroutineScope) {
}
@Composable
fun Screen1() {
val coroutineScope = rememberCoroutineScope()
val vm = remember(coroutineScope) { Screen1ViewModel(coroutineScope) }
}
class Screen2ViewModel(val viewModelScope: CoroutineScope) {
}
@Composable
fun Screen2() {
val coroutineScope = rememberCoroutineScope()
val vm = remember(coroutineScope) { Screen2ViewModel(coroutineScope) }
}
Casey Brooks
01/09/2025, 7:13 PMclass Screen1ViewModel(val viewModelScope: CoroutineScope) {
val component1Visible by mutableStateOf(false)
val component2Visible by mutableStateOf(false)
}
@Composable
fun Screen1() {
val coroutineScope = rememberCoroutineScope()
val vm = remember(coroutineScope) { Screen1ViewModel(coroutineScope) }
if(vm.component1Visible) { Component1() }
if(vm.component2Visible) { Component2() }
}
class Component1ViewModel(val viewModelScope: CoroutineScope) {}
@Composable
fun Component1() {
val coroutineScope = rememberCoroutineScope()
val vm = remember(coroutineScope) { Component1ViewModel(coroutineScope) }
}
class Component2ViewModel(val viewModelScope: CoroutineScope) {}
@Composable
fun Screen2() {
val coroutineScope = rememberCoroutineScope()
val vm = remember(coroutineScope) { Component2ViewModel(coroutineScope) }
}