Mark
02/14/2025, 11:45 AMOnBackPressedCallback
to the NH2 composable and it’s only enabled when NH2 is visible. This works fine except for the fact the predictive animations don’t work if you just override handleOnBackPressed
. I know you can just implement functions like handleOnBackProgressed
but it would be much better to reuse existing logic (which must already be in the androidx activity library) instead. Any pointers?Stylianos Gakis
02/14/2025, 3:06 PMOnBackPressedCallback
yourself at all?
NavHost itself has a PredictiveBackHandler
internally which handles this for you.Stylianos Gakis
02/14/2025, 3:07 PMMark
02/15/2025, 2:03 AMMark
02/15/2025, 2:06 AMMark
02/15/2025, 2:10 AMMark
02/16/2025, 2:46 AMStylianos Gakis
02/16/2025, 11:19 AMMark
02/16/2025, 11:47 AMStylianos Gakis
02/16/2025, 4:24 PMMark
02/17/2025, 1:50 AMStylianos Gakis
02/17/2025, 8:45 AMMark
02/17/2025, 9:03 AMStylianos Gakis
02/17/2025, 9:12 AMMark
02/17/2025, 9:28 AMMark
02/17/2025, 10:24 AMvar mainNavHostAdded by remember {
mutableStateOf(false)
}
Column(modifier = modifier) {
appBar()
if (mainNavHostAdded) {
otherNavHost()
}
mainNavHost()
mainNavHostAdded = true
}
Mark
02/18/2025, 2:21 AMmainNavHostAdded
be set in something like onGloballyPositioned()
? Also, would it be a mistake to use rememberSaveable
?
var mainNavHostAdded by remember {
mutableStateOf(false)
}
Column(modifier = modifier) {
appBar()
if (mainNavHostAdded) {
otherNavHost()
}
Box(modifier = Modifier.onGloballyPositioned { mainNavHostAdded = true }) {
mainNavHost()
}
}
Stylianos Gakis
02/18/2025, 7:52 AMMark
02/18/2025, 8:14 AMSideEffect
? And what is the technical reason why the hack of just putting mainNavHostAdded = true
directly in the composable, shouldn’t be done? If using SideEffect
where should it go? Before, after or inside the Column
, or it doesn’t matter?Stylianos Gakis
02/18/2025, 8:54 AMLaunchedEffect(Unit)
Regarding why you don't want to write to a mutable state in composition after it was read, the reason is described here https://developer.android.com/develop/ui/compose/performance/bestpractices#avoid-backwardsStylianos Gakis
02/18/2025, 8:58 AMLayout(
{
AppBar()
MainNavHost()
OtherNavHost()
}
) {
...measure
layout() {
appbar.place()
otherNavHost.place()
mainNavHost.place()
}
}
So that they are added in composition in the order you wish, but they are rendered in the order that they must.
Here are some docs on custom layouts.Mark
02/18/2025, 9:41 AMrememberSaveable
part? I think it’s important to only use remember
otherwise screen rotation would incorrectly lead to the other host being added before the main host.