This is a dialog fragment that inflates a xml file. This dialog fragment will then be used in another fragment as a pop up.
I am trying to override the back button where the back button should not close/dismiss the dialog fragment. it should only passes a live data through a view model and toast a message “Back Button Pressed! You can’t exit at here”.
Currently when i pressed the back button, the dialog fragment will still dismiss. How can i solve this issue?
class MsicUpdateLater : DialogFragment() {
private val sharedViewModel: BrnViewModel by activityViewModels()
private lateinit var backPressedCallback: OnBackPressedCallback
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(STYLE_NORMAL, R.style.FullWidthSheetDialog)
backPressedCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
// is this the right way?
Toast.makeText(requireContext(), "Back Button Pressed! You can't exit at here", Toast.LENGTH_SHORT).show()
sharedViewModel.msicDialogFragment()
}
}
requireActivity().onBackPressedDispatcher.addCallback(this, backPressedCallback)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.brn_tin_update_later_pop_up, container, false)
}
override fun onDestroyView() {
super.onDestroyView()
backPressedCallback.remove()
}
}