Jordi Saumell
11/17/2020, 10:46 PMnavBackStackEntry.lifecycle.addObserver(object: LifecycleEventObserver {
override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
if (event == Lifecycle.Event.ON_DESTROY) {
koinScope.close()
}
}
})
The problem is that this is run on every recomposition, so I end up having a lot of observers. It works (the scope will be closed when it needs to be) but it is not ideal having several useless observers being called.
What can I do to only add an observer the first time?Ian Lake
11/18/2020, 1:05 AMonCommit(navBackStackEntry)
which will only run when the inputs (i.e., the NavBackStackEntry
instance changes (which it doesn't, so this only runs once)Jordi Saumell
11/18/2020, 9:52 PMcomposable(ScreenA) {…}
composable(ScreenB) {
onCommit(it) {…}
…
}
composable(ScreenC) {…}
With this navigation flow, the onCommit block is called twice.Ian Lake
11/18/2020, 9:56 PMNavBackStackEntry
, are you getting a different instance? Or is it the same instance and there's something wrong with onCommit
?Jordi Saumell
11/19/2020, 9:12 AMIan Lake
11/19/2020, 2:41 PMNavBackStackEntry
since it intentionally doesn't override equals (object equality is enough), but what you're seeing is certainly unexpected - any call to navigate
will create a new instance. Do you mind filing a bug with a reproduction project? https://issuetracker.google.com/issues/new?component=409828&template=1093757Jordi Saumell
11/19/2020, 7:23 PMIan Lake
11/19/2020, 7:30 PM