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

Daniel Singer

03/01/2023, 2:03 AM
The Material3
ModalBottomSheet
example uses
invokeOnCompletion
to wait until the bottom sheet is hidden here Is this materially different than just doing:
Copy code
scope.launch {
  bottomSheetState.hide()
  if(!bottomSheetState.isVisible) {
    openBottomSheet = false
  }
}
since
hide()
seems to suspend until it's done anyway?
a

Albert Chang

03/01/2023, 4:29 AM
invokeOnCompletion
is like
finally
, which will run even when an exception was thrown or the coroutine was cancelled.
a

Andy Himberger

03/01/2023, 4:52 AM
I was finding I had to use invokeOnCompletion myself too so I can run my onDismissRequest logic, as the m3 bottomsheet wasn't calling it for me when I did a hide() from a backhandler (which is also odd that I had to setup my own backhandler to close bottomsheets on back button). though that was alpha06 haven't seen if alpha07 changed things there
j

jossiwolf

03/01/2023, 7:37 AM
Keep in mind that coroutines can be cancelled. If the animation gets interrupted for any reason, you'll still want to remove the sheet from composition if it went to the hidden state. In your code, you are launching a coroutine in which you call hide, a suspend function. If it gets cancelled, the whole coroutine will get cancelled too and you'll never reach the if. invokeOnCompletion on the job and try/finally around the hide call are equivalent for this use case, so it's up to preference
@Andy Himberger M3 sheets have a back handler, you don't need your own. Please file an issue if anything is not working as expected:)
d

Daniel Singer

03/01/2023, 8:45 PM
20 Views