Status Update
Comments
ch...@gmail.com <ch...@gmail.com> #2
Jetpack Compose release version: 1.0.3
Android Studio Build: Android Studio Arctic Fox | 2020.3.1 Patch 2 Build #AI-203.7717.56.2031.7678000, built on August 26, 2021
Kotlin version: 1.5.30
Steps to Reproduce:
- Create a NavHost, and add it as a root composable in Activity
- Add a lifecyle observer using Disposable effect in start destination composable
- Lifecycle event ON_STOP is triggered for a brief moment followed by ON_START event
Please find below a code sample
class AccountActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val navController = rememberNavController()
SampleTheme {
NavHost(navController, "start_route") {
composable("start_route") {
AccountContent()
}
}
}
}
}
}
@Composable
fun AccountContent(lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current) {
DisposableEffect(lifecycleOwner) {
val observer = LifecycleEventObserver { _, event ->
if (event == Lifecycle.Event.ON_START) {
Log.d("Lifecycle", "OnStart View")
} else if (event == Lifecycle.Event.ON_STOP) {
Log.d("Lifecycle", "OnStop View")
}
}
lifecycleOwner.lifecycle.addObserver(observer)
onDispose {
lifecycleOwner.lifecycle.removeObserver(observer)
}
}
Column(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colors.background)
.wrapContentSize(Alignment.Center)
) {
Text(
text = "Account Body",
fontWeight = FontWeight.Bold,
modifier = Modifier.align(Alignment.CenterHorizontally),
textAlign = TextAlign.Right,
fontSize = 25.sp
)
}
}
When we run the app we get following logs for Lifecyle events which we are logging above:
2021-10-19 16:35:34.579 19885-19885/com.example.composesample D/Lifecycle: OnStart View
2021-10-19 16:35:34.580 19885-19885/com.example.composesample D/Lifecycle: OnStop View
2021-10-19 16:35:34.581 19885-19885/com.example.composesample D/Lifecycle: OnStart View
As you can see Lifecycle ON_STOP event is being triggered before triggering ON_START again. This is not a normal behaviour as it seems the view is created 2 times, and the first creation is killed and recreated again.
This issue doesn't occur if we wrap the NavHost inside a Scaffold like below
lass AccountActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val navController = rememberNavController()
SampleTheme {
Scaffold {
NavHost(navController, "start_route") {
composable("start_route") {
AccountContent()
}
}
}
}
}
}
}
In case of above code, we only get single ON_START event and no ON_STOP event in the Lifecycle observer of start destination composable which is kinda expected behaviour. But strangely this behaviour only happens with Scaffold and not any other composable as root like Box, or Surface.
il...@google.com <il...@google.com> #4
Do you have a sample project that reproduces your issue?
ch...@gmail.com <ch...@gmail.com> #5
Hello Ian,
I have attached the sample project below.
Just run the app and check the logs with tag "Lifecyle" and observe that ON_STOP event gets fired at the beginning for some reason. Also added comment in the code what fix this behavior like I have explained above.
Let me know if you need anything else from my side or any further explanation of the issue.
ap...@google.com <ap...@google.com> #6
Branch: androidx-main
commit f88d94e1063eab5d615586fa118bf96652bb57a6
Author: Jeremy Woods <jbwoods@google.com>
Date: Tue Oct 26 15:37:35 2021
Fix NavBackStackEntry initial Lifecycle state
NavBackStackEntry currently relies on its host lifecycle owner to
determine the initial host lifecycle state of the entry. This can cause
issues cause the host state might not reflect the dispatched state, so
we can end up with a scenario where the entry moves to a state before
the NavController's lifecycleObserver has dispatched that state.
We should instead make NavBackStackEntries host lifecycle state rely
directly on NavController's lifecycleObserver's dispatched state. This
sure that the entries lifecycle is always properly in sync.
RelNote: "NavBackStackEntries will not longer be pushed down to
`Lifecycle.State.CREATED` after moving to `Lifecycle.State.STARTED` when
the `NavHost` is added directly to the activity's `setContent()`."
Test: NavHostTest
Bug: 203536683
Change-Id: Ia5ac1c2c7e83f90780dd85187519c08debaa6eca
M navigation/navigation-runtime/src/androidTest/java/androidx/navigation/NavBackStackEntryTest.kt
M navigation/navigation-runtime/src/main/java/androidx/navigation/NavController.kt
M navigation/navigation-testing/src/main/java/androidx/navigation/testing/TestNavigatorState.kt
M navigation/navigation-compose/src/androidTest/java/androidx/navigation/compose/NavHostTest.kt
M navigation/navigation-runtime/src/main/java/androidx/navigation/NavBackStackEntryState.kt
M navigation/navigation-common/src/main/java/androidx/navigation/NavBackStackEntry.kt
jb...@google.com <jb...@google.com> #7
This has been fixed internally and will be available in the Navigation 2.4.0-beta02
release.
Description
Description has been deleted.