I've a peculiar use case where I need to call show() for a BottomSheetDialogFragment and not want it to show on screen until a condition is met. In order to do this I set the following in onCreateDialog()
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
val behavior = dialog.behavior
behavior.isHideable = true
behavior.state = BottomSheetBehavior.STATE_HIDDEN
behavior.peekHeight = 0
return dialog
}
But I see the screen darkened and the bottom sheet hugging the bottom of the screen with 0 height. I can call dialog.hide() in setOnShowListener to get the artifact to hide but this is obviously is causing a screen flickering. Is there a way to make the dialog get created but stay hidden without any changes to the view?
It's too early to set the
BottomSheetBehaviorstate during dialog creation.In documentaion:
So, it assumes that the bottom sheet is already created.
To solve this, it can be transferred to
onStart()callback: