diff --git a/ioBeforeTransition/app/src/main/java/io/github/aniokrait/androidtoybox/ui/MyNavigationCompose.kt b/ioBeforeTransition/app/src/main/java/io/github/aniokrait/androidtoybox/ui/MyNavigationCompose.kt index 8e9fada..6f456fa 100644 --- a/ioBeforeTransition/app/src/main/java/io/github/aniokrait/androidtoybox/ui/MyNavigationCompose.kt +++ b/ioBeforeTransition/app/src/main/java/io/github/aniokrait/androidtoybox/ui/MyNavigationCompose.kt @@ -2,8 +2,10 @@ package io.github.aniokrait.androidtoybox.ui import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.saveable.rememberSaveable +import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.hilt.navigation.compose.hiltViewModel import androidx.navigation.compose.NavHost @@ -25,7 +27,9 @@ fun MyNavigationCompose(modifier: Modifier = Modifier) { val vm: TransitSourceViewModel = hiltViewModel() //Activityが破棄されても保持し続けるためにrememberではなくrememberSaveableで定義 val isLoadCompleted = rememberSaveable { mutableStateOf(false) } + var showIndicator = rememberSaveable { mutableStateOf(false) } TransitSourceScreen( + showIndicator = showIndicator, transit = { //navController.navigateではなく、BooleanのMutableStateを渡す vm.fetchSomeData(isLoadCompleted) @@ -34,7 +38,9 @@ fun MyNavigationCompose(modifier: Modifier = Modifier) { //isLoadCompletedがtrueになったら画面遷移を行う。 LaunchedEffect(isLoadCompleted.value) { if (isLoadCompleted.value) { + showIndicator.value = false navController.navigate("transitTarget") + isLoadCompleted.value = false } } } diff --git a/ioBeforeTransition/app/src/main/java/io/github/aniokrait/androidtoybox/ui/screen/TransitSourceScreen.kt b/ioBeforeTransition/app/src/main/java/io/github/aniokrait/androidtoybox/ui/screen/TransitSourceScreen.kt index 115f7be..96c1faf 100644 --- a/ioBeforeTransition/app/src/main/java/io/github/aniokrait/androidtoybox/ui/screen/TransitSourceScreen.kt +++ b/ioBeforeTransition/app/src/main/java/io/github/aniokrait/androidtoybox/ui/screen/TransitSourceScreen.kt @@ -8,6 +8,7 @@ import androidx.compose.material3.Button import androidx.compose.material3.CircularProgressIndicator import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.MutableState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.saveable.rememberSaveable @@ -18,19 +19,19 @@ import androidx.compose.ui.graphics.Color @Composable fun TransitSourceScreen( + showIndicator: MutableState, transit: () -> Unit ) { - var showIndicator by rememberSaveable { mutableStateOf(false) } Column { Button(onClick = { - showIndicator = true + showIndicator.value = true transit() }) { Text("遷移する") } } - if(showIndicator) { + if(showIndicator.value) { Box(modifier = Modifier .fillMaxSize() .background(color = Color.Black.copy(alpha = 0.3f))