Basics for System Back

Android Developers · 27K views · 2022 · 14:11

Open on YouTube

Software (other)Web developmenttalkdated

Why Stack Overflow recommends it

Stack Overflow users link it when explaining that back callbacks should be enabled and disabled ahead of time rather than checked just in time, and for the drawer and onBackPressed deprecation answers that follow from it.

Recommended by 2 people across 2 different months, 2022 to 2023.

What it is

A Google I/O 2022 session from the Android Developers channel in which Shan Huang and Nick Bearman explain Android 13's new system back handling and the predictive back gesture. It covers the OnBackPressedDispatcher and callback model, and how apps can support back animations.

What people said

Recommends Ananthakrishnan K R · 2022-09 · Stack Overflow · on “Close drawer navigation view on backpressed while using jetpack nagivation ui components”

According to this I/O talk [this video] , It is recommended to enable the callback when drawer is open and disable when drawer is closed. val callback = onBackPressedDispatcher.addCallback(this, false) { binding.drawerLayout.closeDrawer(GravityCompat.START) } binding.drawerLayout.addDrawerListener(object : DrawerListener { override fun onDrawerOpened(drawerView: View) { callback.isEnabled = true } override fun onDrawerClosed(drawerView: View) { callback.isEnabled = false } override fun onDrawerSlide(drawerView: View, slideOffset: Float) = Unit override fun onDrawerStateChanged(newState: Int) = Unit }) Here is the Offical documentation [LINK]

Recommends ianhanniballake · 2023-02 · Stack Overflow · on “Proper usage of OnBackPressedDispatcher”

As per the Basics of System Back video [this video] , the whole point of the Predictive Back gesture [LINK] is to know ahead of time what is going to handle back. That means that you should never have just-in-time logic such as checkSomeCondition as part of your call to handleOnBackPressed . Instead, as explained in the Custom back navigation documentation [LINK] , you should be updating the isEnabled state of your OnBackPressedCallback ahead of time whenever the conditions you used to check in checkSomeCondition changed. This ensures that your callback is only invoked when your condition is already true and is disabled when the condition is false, thus allowing the default behavior to occur. cla

Cites Ananthakrishnan K R · 2022-09 · Stack Overflow · on “onBackPressed() is deprecated. What is the alternative?”

Bonus: To close DrawerLayout when onBackPressed use like below (according to this I/O talk [this video] ), val callback = onBackPressedDispatcher.addCallback(this, false) { binding.drawerLayout.closeDrawer(GravityCompat.START) } binding.drawerLayout.addDrawerListener(object : DrawerListener { override fun onDrawerOpened(drawerView: View) { callback.isEnabled = true } override fun onDrawerClosed(drawerView: View) { callback.isEnabled = false } override fun onDrawerSlide(drawerView: View, slideOffset: Float) = Unit override fun onDrawerStateChanged(newState: Int) = Unit })

Cites kunal singhal · 2023-07 · Stack Overflow · on “Drawer Layout not closing on back pressed (depending on support-v4 lib)”

if you want to close the drawer onBackPressed() but the function is not even getting called when the back button is pressed and one more condition you are using latest version android 13 then you should try adding below code in your OnCreate() or whatever you want to use and for more android 13 [this video] val callback = onBackPressedDispatcher.addCallback(this, false) { drawerLayout.closeDrawer(GravityCompat.START) } drawerLayout.addDrawerListener(object : DrawerLayout.DrawerListener { override fun onDrawerOpened(drawerView: View) { callback.isEnabled = true } override fun onDrawerClosed(drawerView: View) { callback.isEnabled = false } override fun onDrawerSlide(drawerView: View, slideOffset: F

Cites Daniel Hritzkiv · 2023-10 · Stack Overflow · on “Android predictive back compose navigation”

In the video "Basics for System Back" [this video] at 13:07, the presenter seems to suggest that back previews (aka the peak animation) is not supported yet when using Jetpack Compose or Fragment navigation. I believe only the back to home gesture animation is possible for Jetpack Compose or Fragment-based apps. It's frustrating that this is buried deep at the end of a video, and not in easily indexable, and easily update-able text based documentation.

Cites ianhanniballake · 2023-10 · Stack Overflow · on “Should calls to onBackPressed() be treated differently from API 33?”

The whole point of the ahead of time model for handling system back, if you had really watched the video linked in the guide [this video] , is to enable and disable your callback whenever your state changes so that you only get a callback when you can handle back - you never do any conditional checking just in time.