Graceful Exits: Implementing Close Confirmation for Jetpack Compose ModalBottomSheet | by Katie Barnett | Jan, 2024
The ModalBottomSheet in Jetpack Compose is easy to use, it just pops up at the bottom of the screen and allows the user easy interaction without covering the whole screen and blocking content.
The problem is, when the user accidentally taps out of the sheet or closes it they may lose any input they have added or changes they have made.
If we were using a custom close button then it would be easy, simply show a dialog when the user presses the button and only perform the sheet hide when the user has confirmed. But what about the bottom sheet handle, swiping down the bottom sheet or tapping outside on the scrim?
Fear not! It is pretty straight forward.
First, we have the basic building blocks of our ModalBottomSheet
:
Here we have a simple Boolean
state to hold whether the ModalBottomSheet
should be displayed or not, showAddSheet
, the ModalBottomSheet
also takes in addSheetState
which is initialised & tracked with rememberModalBottomSheetState
.
We also need to create a AlertDialog
to be the confirmation dialog to show when the user tries to close the ModalBottomSheet
:
Both the dialog and the save button in the ModalBottomSheet
includes the closing of the ModalBottomSheet
using the addSheetState.hide()
method to allow the hide animation to run before fully removing the sheet from the composition via the showAddSheet
state.
Observe the value change
Now we have the basic components the next thing to do is look at the rememberModalBottomSheetState
. This takes in a callback confirmValueChange
which we can confirm or veto a pending sheet state change. The change here is simple, we need to check if the requested sheet value is Hidden
(the other values are Expanded
and PartiallyExpanded
— we don’t care about these when showing the confirmation dialog) and if so, show the dialog. Then we need to return a Boolean
to indicate whether the sheet should take the requested action or not.
So, since we want to show the AlertDialog
we just set the showConfirmChanges
state to true
and return false
from the confirmValueChange
callback to prevent the action being carried out. If the requested sheet state value is not Hidden
then just return true
so it is carried out unaffected.
This will be run whenever the user touches outside of the ModalBottomSheet
or tries to swipe down on it using the handle. It won’t get called if a standard back press is used (as that is handled by the navigation architecture) but that can be overridden in a similar manner using OnBackPressedDispatcher
.
And that’s it! Simple!
Finally, we have the full solution:
Tags In
Related Posts
Leave a Reply Cancel reply
Categories
- ! Без рубрики (1)
- ++PU (1)
- 1 (1)
- 1w (1)
- 1win Brazil (1)
- 1win India (1)
- 1WIN Official In Russia (1)
- 1win Turkiye (1)
- 1xbet egypt (1)
- 2ankarafayansustasi.net_may (1)
- ankarafayansustasi.netsiteai apr (1)
- Artificial intelligence (1)
- Arts & Entertainment, Photography (1)
- belugasitesi_mAY (1)
- BH_TOPsitesi apr (1)
- BHsitesy_may (2)
- Blog (3)
- Bookkeeping (14)
- Bootcamp de programação (2)
- Bootcamp de programación (2)
- BT_TOPsitesi apr (1)
- casino (5)
- casinom-hub (1)
- casinom-hub.comsitesi apr (3)
- colombian mail order brides (1)
- Cryptocurrency exchange (2)
- Dinamobet_next (1)
- Disease & Illness, Colon Cancer (1)
- Dumanbet (1)
- Dumanbet_next (1)
- Finance, Insurance (1)
- FinTech (5)
- Forex Trading (11)
- Galabet (1)
- Health & Fitness, Fitness Equipment (1)
- Hitbet (1)
- Home & Family, Crafts (1)
- Home & Family, Gardening (1)
- Internet Business, Audio-Video Streaming (1)
- Internet Business, Ecommerce (1)
- Internet Business, Email Marketing (1)
- Internet Business, Internet Marketing (1)
- IT Вакансії (1)
- IT Образование (5)
- IT Освіта (1)
- latin women dating (1)
- mail order bride (1)
- Mars bahis (2)
- Matadorbet (1)
- minimiri.comsitesi apr (3)
- Mobile App Development (771)
- Mostbet Russia (1)
- New Post (1)
- News (12)
- PB_TOPsitesi apr (1)
- PBsitesi_may (1)
- Pusulabet (1)
- redmirepool.bizsitesi apr (2)
- redmirepoolsitesi_may (1)
- Reference & Education, College (1)
- Reference & Education, Sociology (1)
- Rokusitesi apr (1)
- Sober living (6)
- Society, Divorce (1)
- Software development (7)
- Superbetin (1)
- Tempobet_next (1)
- thelongeststride.comsitesi apr (1)
- tipobet-turkiyesitesi apr (1)
- Ultrabet (1)
- Uncategorized (1)
- Игра (2)
- казино (1)
- Криптовалюты (1)
- Новости Криптовалют (1)
- Финтех (7)
- Форекс Брокеры (9)
- Форекс обучение (2)