I use PopupWindow
to show menu in Fragment
and then navigate with Navigation Compopnent
to another Fragment
override fun createPopupMenu(popupView: View?) {
if (popupView == null)
return
menuPopup = PopupWindow(context).apply {
elevation = requireContext().convertDpToPixel(10f).toFloat()
setBackgroundDrawable(null)
height = ViewGroup.LayoutParams.WRAP_CONTENT
width = ViewGroup.LayoutParams.WRAP_CONTENT
contentView = popupView
isOutsideTouchable = true
}
}
When I try to close it with menuPopup.dismiss()
, it is not always work's because internall check of isShowing()
returns false. But PopupWindow
visible and closes on touch outside.
Show method is:
override fun showMenu() {
if (::menuPopup.isInitialized) {
menuPopup.showAsDropDown(
some_menu_button,
requireContext().convertDpToPixel(if (requireContext().isRTL()) -185f else 0f),
requireContext().convertDpToPixel(8f)
)
}
}