I need to close (finish) the current Activity when back-pressed in Dialog Fragment. Before I used the below code and it worked. But now onBackPressed is deprecated.
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return object : Dialog(activity!!, theme) {
override fun onBackPressed() {
super.onBackPressed()
activity?.onBackPressed()
}
}
}
I want to migrate onBackPressed and implemented onBackPressedDispatcher as below, but handleOnBackPressed is not calling.
override fun onAttach(context: Context) {
super.onAttach(context)
val backPressedCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
// code snippet for close Activity
}
}
requireActivity().onBackPressedDispatcher.addCallback(this, backPressedCallback)
}
How can I modify this code to close Activity when back-press in Dialog Fragment?