Upgrade DrawerLayout to 1.2.0 and simplify code (#4412)
The main benefit of upgrading to version 1.2.0 of `DrawerLayout` is ~~to properly support **predictive back animations**: when initiating a back gesture on API 33+, the DrawerLayout will animate automatically~~ to handle back navigation automatically. The predictive back animation of the menu however depends on `NavigationView` which is not used in the project. In addition to the upgrade, simplify DrawerLayout integration: - Forward key events to the DrawerLayout so it can intercept them and close itself when needed. - Don't handle the DrawerLayout closing manually using the `OnBackPressedCallback` anymore. This is not necessary since the back event will now be intercepted by the DrawerLayout when needed before reaching the `OnBackPressedDispatcher`. - Remove legacy fix for DrawerLayout staying open after Activity recreation.
This commit is contained in:
parent
2ce03c2e89
commit
11d18e1e70
2 changed files with 12 additions and 30 deletions
|
|
@ -46,11 +46,9 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout
|
|||
import androidx.core.app.ActivityCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.content.pm.ShortcutManagerCompat
|
||||
import androidx.core.view.GravityCompat
|
||||
import androidx.core.view.MenuProvider
|
||||
import androidx.core.view.forEach
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.drawerlayout.widget.DrawerLayout
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.preference.PreferenceManager
|
||||
import androidx.viewpager2.widget.MarginPageTransformer
|
||||
|
|
@ -199,14 +197,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
|
|||
|
||||
private val onBackPressedCallback = object : OnBackPressedCallback(false) {
|
||||
override fun handleOnBackPressed() {
|
||||
when {
|
||||
binding.mainDrawerLayout.isOpen -> {
|
||||
binding.mainDrawerLayout.close()
|
||||
}
|
||||
binding.viewPager.currentItem != 0 -> {
|
||||
binding.viewPager.currentItem = 0
|
||||
}
|
||||
}
|
||||
binding.viewPager.currentItem = 0
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -487,12 +478,14 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
|
|||
}
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
// For some reason the navigation drawer is opened when the activity is recreated
|
||||
if (binding.mainDrawerLayout.isOpen) {
|
||||
binding.mainDrawerLayout.closeDrawer(GravityCompat.START, false)
|
||||
override fun dispatchKeyEvent(event: KeyEvent): Boolean {
|
||||
// Allow software back press to be properly dispatched to drawer layout
|
||||
val handled = when (event.action) {
|
||||
KeyEvent.ACTION_DOWN -> binding.mainDrawerLayout.onKeyDown(event.keyCode, event)
|
||||
KeyEvent.ACTION_UP -> binding.mainDrawerLayout.onKeyUp(event.keyCode, event)
|
||||
else -> false
|
||||
}
|
||||
return handled || super.dispatchKeyEvent(event)
|
||||
}
|
||||
|
||||
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
|
||||
|
|
@ -629,19 +622,6 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
|
|||
)
|
||||
setSavedInstance(savedInstanceState)
|
||||
}
|
||||
binding.mainDrawerLayout.addDrawerListener(object : DrawerLayout.DrawerListener {
|
||||
override fun onDrawerSlide(drawerView: View, slideOffset: Float) { }
|
||||
|
||||
override fun onDrawerOpened(drawerView: View) {
|
||||
onBackPressedCallback.isEnabled = true
|
||||
}
|
||||
|
||||
override fun onDrawerClosed(drawerView: View) {
|
||||
onBackPressedCallback.isEnabled = binding.tabLayout.selectedTabPosition > 0
|
||||
}
|
||||
|
||||
override fun onDrawerStateChanged(newState: Int) { }
|
||||
})
|
||||
}
|
||||
|
||||
private fun refreshMainDrawerItems(
|
||||
|
|
@ -902,7 +882,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
|
|||
|
||||
onTabSelectedListener = object : OnTabSelectedListener {
|
||||
override fun onTabSelected(tab: TabLayout.Tab) {
|
||||
onBackPressedCallback.isEnabled = tab.position > 0 || binding.mainDrawerLayout.isOpen
|
||||
onBackPressedCallback.isEnabled = tab.position > 0
|
||||
|
||||
binding.mainToolbar.title = tab.contentDescription
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue