https://kotlinlang.org logo
#compose
Title
# compose
t

Tomas Škarnulis

09/23/2023, 10:30 AM
Found interesting behaviour of Material3
BottomSheetScaffold
and
NavHost
App is using
NavHost
that has nested navigation inside where few screens will be attached to main
NavHost
, and other smaller one - to
BottomSheetScaffold
NavHost
In first init of the screen bottom sheet can not be hidden on swipe. Then, if we navigate from nav graph that has bottom sheet to other navigation - on back
BottomSheetScaffold
can be hidden. 😄 Other thing, if we leave screen with
BottomSheetScaffold
expanded - on back we will get :
Copy code
IllegalStateException - If the offset has not been initialized yet
I am assuming that
NavHost
somehow recreates screen maybe, and
rememberStandardBottomSheetState
initialised again... Still figuring this out, just wanted to share maybe someone already solved similar situation to this and can give some tips where to look
Quick
LaunchedEffect(Unit)
show that states is really recreated on back:
Copy code
MapAndBottomSheet: creation
MapAndBottomSheet: botSheet 131380672
MapAndBottomSheet: scaffold: 124916473

....

MapAndBottomSheet: creation
MapAndBottomSheet: botSheet 195691014
MapAndBottomSheet: scaffold: 178577607
So i hacked my way like this:
rememberStandardBottomSheetState
set skipHiddenState to false - this will always let bottom sheet to be hidden on swipe Than use
rememberSaveable
to save last know
SheetValue
on
confirmValueChange
in
rememberStandardBottomSheetState
than I use
LaunchedEffect(lastBootomSheetState)
and check last and current sheet value, and if its not a same - I use last value to move bottom sheet + save last = current Hacky, so will check if it has any issues here
Screen Recording 2023-09-23 at 15.55.56.mov
p

Patrick Steiger

09/23/2023, 10:11 PM
It’s been a while since I used NavHost but the assumption that previous nav entries are destroyed and recreated is correct (at least it was back then) Navigation relies on SavedState mechanism to restore state when navigating back to some destinations
j

jossiwolf

09/25/2023, 1:47 PM
Which version is this on?
t

Tomas Škarnulis

09/25/2023, 2:46 PM
Copy code
androidx.compose.material3:material3:1.2.0-alpha08
androidx.navigation:navigation-compose:2.7.0-beta02
Kotlin version: 1.8.20
j

jossiwolf

09/25/2023, 2:56 PM
Please file an issue with a repro!
3 Views