I want to add on-click listener to onBackPressed() button. How can I do that?
fun onBackPressed(it: View) {
val title = binding.edittexttitle.text
val notes = binding.edittextnote.text
val d = Date()
val s: CharSequence = DateFormat.format("MMMM d, yyyy ", d.time)
Log.e("@@@@@", "createNotes: $s")
}
2
Answers
You can override handleOnBackPressed.
To override
onBackPressed
infragment
callonBackPressedDispatcher
of theactivity
and add acallback
to it. In callback put whatever code you want to be get executed on the back button press.Afterwards if you want the
back button
to work as it normally does, just disablecallback
usingisEnabled = false
and then callrequireActivity().onBackPressed()
, inside callback lambda.