onBackPressed() 已弃用。还有什么选择呢?

2024-02-01

我已经升级了targetSdkVersion and compileSdkVersion to 33.

我现在收到一条警告,告诉我按下后退 https://developer.android.com/reference/android/app/Activity#onBackPressed()已弃用。

我看到使用建议android.window.OnBackInvokedCallback https://developer.android.com/reference/android/window/OnBackInvokedCallback or androidx.activity.OnBackPressedCallback https://developer.android.com/reference/androidx/activity/OnBackPressedCallback来处理后退导航。任何人都可以帮助我使用更新的方法吗?

Example

Use Case

I use if (isTaskRoot) {}在 - 的里面onBackPressed() https://developer.android.com/reference/android/app/Activity#onBackPressed()方法检查该活动是否是活动堆栈上的最后一个活动。

override fun onBackPressed() {
    if (isTaskRoot) { // Check whether this activity is last on the activity stack. (Check whether this activity opened from a Push Notification.)
        startActivity(Intent(mContext, Dashboard::class.java))
        finish()
    } else {
        finishWithResultOK()
    }
}

将 onBackPressed() 替换为以下代码。

onBackPressedDispatcher.onBackPressed()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

onBackPressed() 已弃用。还有什么选择呢? 的相关文章

随机推荐