Created
March 18, 2023 19:48
-
-
Save hoc081098/83822661ab0cee3648eace5ce22803fe to your computer and use it in GitHub Desktop.
rememberSaveableStateHolder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.hoc081098.kmpviewmodelsample.android | |
import androidx.compose.foundation.layout.Arrangement | |
import androidx.compose.foundation.layout.Box | |
import androidx.compose.foundation.layout.Column | |
import androidx.compose.foundation.layout.Row | |
import androidx.compose.foundation.layout.fillMaxSize | |
import androidx.compose.material3.Button | |
import androidx.compose.material3.Text | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.DisposableEffect | |
import androidx.compose.runtime.getValue | |
import androidx.compose.runtime.mutableStateOf | |
import androidx.compose.runtime.saveable.rememberSaveable | |
import androidx.compose.runtime.saveable.rememberSaveableStateHolder | |
import androidx.compose.runtime.setValue | |
import androidx.compose.ui.Modifier | |
@Composable | |
fun <T : Any> Navigation( | |
currentScreen: T, | |
modifier: Modifier = Modifier, | |
content: @Composable (T) -> Unit | |
) { | |
// create SaveableStateHolder. | |
val saveableStateHolder = rememberSaveableStateHolder() | |
Box(modifier) { | |
// Wrap the content representing the `currentScreen` inside `SaveableStateProvider`. | |
// Here you can also add a screen switch animation like Crossfade where during the | |
// animation multiple screens will be displayed at the same time. | |
saveableStateHolder.SaveableStateProvider(currentScreen) { | |
content(currentScreen) | |
} | |
} | |
} | |
@Composable | |
fun Demo123Screen() { | |
Column { | |
var screen by rememberSaveable { mutableStateOf("screen1") } | |
Row(horizontalArrangement = Arrangement.SpaceEvenly) { | |
Button(onClick = { screen = "screen1" }) { | |
Text("Go to screen1") | |
} | |
Button(onClick = { screen = "screen2" }) { | |
Text("Go to screen2") | |
} | |
} | |
Navigation(screen, Modifier.fillMaxSize()) { currentScreen -> | |
if (screen == "screen1") { | |
Screen1() | |
} else { | |
Screen2() | |
} | |
} | |
} | |
} | |
@Composable | |
fun Screen1() { | |
DisposableEffect(Unit) { | |
onDispose { | |
println("Screen1 disposed") | |
} | |
} | |
var count by rememberSaveable { mutableStateOf(0) } | |
Column { | |
Text(text = "Screen1: count=$count") | |
// increase button | |
Button(onClick = { count++ }) { | |
Text("Increase") | |
} | |
} | |
} | |
@Composable | |
fun Screen2() { | |
DisposableEffect(Unit) { | |
onDispose { | |
println("Screen2 disposed") | |
} | |
} | |
var count by rememberSaveable { mutableStateOf(0) } | |
Column { | |
Text(text = "Screen2: count=$count") | |
// increase button | |
Button(onClick = { count++ }) { | |
Text("Increase") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment