I have upgraded targetSdkVersion
and compileSdkVersion
to 33
.
Now getting warning onBackPressed
is deprecated.
It is suggested to use use OnBackInvokedCallback
or androidx.activity.OnBackPressedCallback
to handle back navigation instead. Anyone can help me to use the updated method.
Example:
Use Case: I use if (isTaskRoot) {}
inside onBackPressed(){}
method to check activity is last on the activity-stack.
override fun onBackPressed() {
if (isTaskRoot) { // Check this activity is last on the activity-stack.(Check Whether This activity opened from Push-Notification)
startActivity(Intent(mContext, Dashboard::class.java))
finish()
} else {
finishWithResultOK()
}
}
super.onBackPressed()
, then you can just remove the method entirely as you're not writing any custom back behavior. If you are writing custom back behavior, show your code.if (isTaskRoot) {}
insideonBackPressed()
to check activity is last on the activity-stack. I have also updated the question.onBackPressed().
isTaskRoot
conditional? Most the answer seem to be very focused on trivial use-cases.