Fix destination reset after configuration change

Also enable predictive back gesture for Gallery App.

Bug: 244122804
Test: Manual with Gallery App
Change-Id: I68bb694c87b2614aa908f1e973aea8fa7288f47c
This commit is contained in:
Chaohui Wang
2022-09-14 15:45:39 +08:00
parent 826ce0df09
commit 098ecd9dbf
3 changed files with 21 additions and 9 deletions

View File

@@ -20,7 +20,8 @@
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_label"
android:supportsRtl="true">
android:supportsRtl="true"
android:enableOnBackInvokedCallback="true">
<activity
android:name=".MainActivity"
android:exported="true">

View File

@@ -59,6 +59,7 @@ android {
}
dependencies {
api "androidx.appcompat:appcompat:1.6.0-rc01"
api "androidx.compose.material3:material3:$jetpack_compose_material3_version"
api "androidx.compose.material:material-icons-extended:$jetpack_compose_version"
api "androidx.compose.runtime:runtime-livedata:$jetpack_compose_version"

View File

@@ -22,7 +22,10 @@ import androidx.activity.compose.setContent
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
@@ -50,9 +53,6 @@ open class BrowseActivity(
@Composable
private fun MainContent() {
val destination =
intent?.getStringExtra(KEY_DESTINATION) ?: sppRepository.getDefaultStartPageName()
val navController = rememberNavController()
CompositionLocalProvider(navController.localNavController()) {
NavHost(navController, ROOT_PAGE_NAME) {
@@ -70,13 +70,23 @@ open class BrowseActivity(
}
}
}
}
InitialDestinationNavigator(navController)
}
@Composable
private fun InitialDestinationNavigator(navController: NavHostController) {
val destinationNavigated = rememberSaveable { mutableStateOf(false) }
if (destinationNavigated.value) return
destinationNavigated.value = true
LaunchedEffect(Unit) {
val destination =
intent?.getStringExtra(KEY_DESTINATION) ?: sppRepository.getDefaultStartPageName()
if (destination.isNotEmpty()) {
LaunchedEffect(Unit) {
navController.navigate(destination) {
popUpTo(navController.graph.findStartDestination().id) {
inclusive = true
}
navController.navigate(destination) {
popUpTo(navController.graph.findStartDestination().id) {
inclusive = true
}
}
}