We use CMP in prod on iOS and Android. I want drop...
# compose
s
We use CMP in prod on iOS and Android. I want dropdowns to close when the user presses the system back button. The only way that I could think of to achieve this is to give each instance of a drop-down a tag, and add/remove the tag from a global set when the drop-down is closed/open or disposed, and listen to a flow of back-presses (emitted in
onBackPressedDispatcher
on Android) in each screen with a drop-down, and close the drop-down if the instance's tag is in the set. That seems like a lot of overhead for a trivial framework-level need, but
onDismissRequest
is not called when the user presses the back button.
s
Does a BackHandler not work when the drop-down is showing to call the dismiss function?
s
@Stylianos Gakis firstly, that is only available on Android (still not sure if iOS even has a back button capability 😳). So, can't include
BackHandler
in common code. Secondly, even on Android, for some reason `onDismissRequest`is not called when the user presses the back button. Thirdly, even if BackHandler was available, it would have to be included in every screen with a drop-down, and maybe even require keeping track of a stack of drop-downs which were exposed if multiple?
s
The BackHandler could be inside the drop-down composable itself, and be enabled only when it is showing, so no need to track anything. Does CMP not have a BackHandler which is a no-op for non Android platforms? If not, I'd imagine adding your own expect-actuals there should work.
And importantly if many BackHandler instances exist, the last one to be added is the one which will consume back press. So if you got that composable inside the DropDown, and enable it when the dropdown does in fact show, that feels like it should just work properly
👍 1
s
Will try