When onBackPressed()
not working in recent sdk, we have to use the new method
onBackPressedDispatcher.onBackPressed()
.
But my question is
will it finish my activity?
Or, I have to callback the onBackPressedDispatcher.onBackPressed()
to finish my activity?
Button code is :
button.setOnClickListener {
onBackPressedDispatcher.onBackPressed()
}
CallBack Code:
onBackPressedDispatcher.addCallback(this, object: OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
finish()
}
})
The answer is :
onBackPressedDispatcher.onBackPressed() will finish my activity
Luckily onBackPressedDispatcher.onBackPressed() have destroyed my activity. I don't need to callback the OnBackPressedCallback object to finish() my activity.
Here is the checking code:
override fun onCreate(savedInstanceState: Bundle?) {
...
...
btnBack?.setOnClickListener { onBackPressedDispatcher.onBackPressed() }
//onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
//override fun handleOnBackPressed() {
//finish() // No Longer Needs to Callback and Finish
//}
//})
}
override fun isFinishing(): Boolean {
println("Finishing")
return super.isFinishing()
}
override fun onDestroy() {
super.onDestroy()
println("Destroyed & Finished")
}