Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

'onBackPressed(): Unit' is deprecated. Overrides deprecated member in 'androidx.core.app.ComponentActivity'. Deprecated in Java

188 views
Skip to first unread message

Jakub

unread,
Jun 28, 2022, 5:37:53 AM6/28/22
to
Welcome

I have this error in this function

override fun onOptionsItemSelected(item: MenuItem): Boolean {
when ( item.itemId ) {
android.R.id.home -> {
onBackPressed()
}
}


'onBackPressed(): Unit' is deprecated. Overrides deprecated member in
'androidx.core.app.ComponentActivity'. Deprecated in Java




targetSdk 33

how to solve this problems?

Calum

unread,
Jun 28, 2022, 7:21:54 AM6/28/22
to
On 28/06/2022 10:38, Jakub wrote:

> 'onBackPressed(): Unit' is deprecated. Overrides deprecated member in
> 'androidx.core.app.ComponentActivity'. Deprecated in Java
>
> targetSdk 33
>
> how to solve this problems?

<https://developer.android.com/reference/android/app/Activity.html>

The documentation literally tells you:

onBackPressed()
This method was deprecated in API level 33. Use OnBackInvokedCallback or
androidx.activity.OnBackPressedCallback to handle back navigation instead.

Starting from Android 13 (API level 33), back event handling is moving
to an ahead-of-time model and Activity#onBackPressed() and
KeyEvent#KEYCODE_BACK should not be used to handle back events (back
gesture or back button click). Instead, an OnBackInvokedCallback should
be registered using Activity#getOnBackInvokedDispatcher()
.registerOnBackInvokedCallback(priority, callback).


See also:

<https://stackoverflow.com/questions/72634225/onbackpressed-deprecated-what-is-the-alternative>

<https://codelabs.developers.google.com/handling-gesture-back-navigation#0>

0 new messages