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
|
@ -46,11 +46,9 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout
|
||||||
import androidx.core.app.ActivityCompat
|
import androidx.core.app.ActivityCompat
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import androidx.core.content.pm.ShortcutManagerCompat
|
import androidx.core.content.pm.ShortcutManagerCompat
|
||||||
import androidx.core.view.GravityCompat
|
|
||||||
import androidx.core.view.MenuProvider
|
import androidx.core.view.MenuProvider
|
||||||
import androidx.core.view.forEach
|
import androidx.core.view.forEach
|
||||||
import androidx.core.view.isVisible
|
import androidx.core.view.isVisible
|
||||||
import androidx.drawerlayout.widget.DrawerLayout
|
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import androidx.preference.PreferenceManager
|
import androidx.preference.PreferenceManager
|
||||||
import androidx.viewpager2.widget.MarginPageTransformer
|
import androidx.viewpager2.widget.MarginPageTransformer
|
||||||
|
@ -199,16 +197,9 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
|
||||||
|
|
||||||
private val onBackPressedCallback = object : OnBackPressedCallback(false) {
|
private val onBackPressedCallback = object : OnBackPressedCallback(false) {
|
||||||
override fun handleOnBackPressed() {
|
override fun handleOnBackPressed() {
|
||||||
when {
|
|
||||||
binding.mainDrawerLayout.isOpen -> {
|
|
||||||
binding.mainDrawerLayout.close()
|
|
||||||
}
|
|
||||||
binding.viewPager.currentItem != 0 -> {
|
|
||||||
binding.viewPager.currentItem = 0
|
binding.viewPager.currentItem = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressLint("RestrictedApi")
|
@SuppressLint("RestrictedApi")
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
@ -487,12 +478,14 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStart() {
|
override fun dispatchKeyEvent(event: KeyEvent): Boolean {
|
||||||
super.onStart()
|
// Allow software back press to be properly dispatched to drawer layout
|
||||||
// For some reason the navigation drawer is opened when the activity is recreated
|
val handled = when (event.action) {
|
||||||
if (binding.mainDrawerLayout.isOpen) {
|
KeyEvent.ACTION_DOWN -> binding.mainDrawerLayout.onKeyDown(event.keyCode, event)
|
||||||
binding.mainDrawerLayout.closeDrawer(GravityCompat.START, false)
|
KeyEvent.ACTION_UP -> binding.mainDrawerLayout.onKeyUp(event.keyCode, event)
|
||||||
|
else -> false
|
||||||
}
|
}
|
||||||
|
return handled || super.dispatchKeyEvent(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
|
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
|
||||||
|
@ -629,19 +622,6 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
|
||||||
)
|
)
|
||||||
setSavedInstance(savedInstanceState)
|
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(
|
private fun refreshMainDrawerItems(
|
||||||
|
@ -902,7 +882,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
|
||||||
|
|
||||||
onTabSelectedListener = object : OnTabSelectedListener {
|
onTabSelectedListener = object : OnTabSelectedListener {
|
||||||
override fun onTabSelected(tab: TabLayout.Tab) {
|
override fun onTabSelected(tab: TabLayout.Tab) {
|
||||||
onBackPressedCallback.isEnabled = tab.position > 0 || binding.mainDrawerLayout.isOpen
|
onBackPressedCallback.isEnabled = tab.position > 0
|
||||||
|
|
||||||
binding.mainToolbar.title = tab.contentDescription
|
binding.mainToolbar.title = tab.contentDescription
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ androidx-browser = "1.8.0"
|
||||||
androidx-cardview = "1.0.0"
|
androidx-cardview = "1.0.0"
|
||||||
androidx-constraintlayout = "2.1.4"
|
androidx-constraintlayout = "2.1.4"
|
||||||
androidx-core = "1.13.1"
|
androidx-core = "1.13.1"
|
||||||
|
androidx-drawerlayout = "1.2.0"
|
||||||
androidx-exifinterface = "1.3.7"
|
androidx-exifinterface = "1.3.7"
|
||||||
androidx-fragment = "1.7.0"
|
androidx-fragment = "1.7.0"
|
||||||
androidx-hilt = "1.2.0"
|
androidx-hilt = "1.2.0"
|
||||||
|
@ -71,6 +72,7 @@ androidx-constraintlayout = { module = "androidx.constraintlayout:constraintlayo
|
||||||
androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "androidx-core" }
|
androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "androidx-core" }
|
||||||
androidx-core-splashscreen = { module = "androidx.core:core-splashscreen", version.ref = "androidx-splashscreen" }
|
androidx-core-splashscreen = { module = "androidx.core:core-splashscreen", version.ref = "androidx-splashscreen" }
|
||||||
androidx-core-testing = { module = "androidx.arch.core:core-testing", version.ref = "androidx-testing" }
|
androidx-core-testing = { module = "androidx.arch.core:core-testing", version.ref = "androidx-testing" }
|
||||||
|
androidx-drawerlayout = { group = "androidx.drawerlayout", name = "drawerlayout", version.ref = "androidx-drawerlayout" }
|
||||||
androidx-emoji2-core = { module = "androidx.emoji2:emoji2", version.ref = "emoji2" }
|
androidx-emoji2-core = { module = "androidx.emoji2:emoji2", version.ref = "emoji2" }
|
||||||
androidx-emoji2-views-core = { module = "androidx.emoji2:emoji2-views", version.ref = "emoji2" }
|
androidx-emoji2-views-core = { module = "androidx.emoji2:emoji2-views", version.ref = "emoji2" }
|
||||||
androidx-emoji2-view-helper = { module = "androidx.emoji2:emoji2-views-helper", version.ref = "emoji2" }
|
androidx-emoji2-view-helper = { module = "androidx.emoji2:emoji2-views-helper", version.ref = "emoji2" }
|
||||||
|
@ -140,7 +142,7 @@ androidx = ["androidx-core-ktx", "androidx-appcompat", "androidx-fragment-ktx",
|
||||||
"androidx-emoji2-core", "androidx-emoji2-views-core", "androidx-emoji2-view-helper", "androidx-lifecycle-viewmodel-ktx",
|
"androidx-emoji2-core", "androidx-emoji2-views-core", "androidx-emoji2-view-helper", "androidx-lifecycle-viewmodel-ktx",
|
||||||
"androidx-constraintlayout", "androidx-paging-runtime-ktx", "androidx-viewpager2", "androidx-work-runtime-ktx",
|
"androidx-constraintlayout", "androidx-paging-runtime-ktx", "androidx-viewpager2", "androidx-work-runtime-ktx",
|
||||||
"androidx-core-splashscreen", "androidx-activity", "androidx-media3-exoplayer", "androidx-media3-datasource-okhttp",
|
"androidx-core-splashscreen", "androidx-activity", "androidx-media3-exoplayer", "androidx-media3-datasource-okhttp",
|
||||||
"androidx-media3-ui"]
|
"androidx-media3-ui", "androidx-drawerlayout"]
|
||||||
filemojicompat = ["filemojicompat-core", "filemojicompat-ui", "filemojicompat-defaults"]
|
filemojicompat = ["filemojicompat-core", "filemojicompat-ui", "filemojicompat-defaults"]
|
||||||
glide = ["glide-core", "glide-okhttp3-integration", "glide-animation-plugin"]
|
glide = ["glide-core", "glide-okhttp3-integration", "glide-animation-plugin"]
|
||||||
material-drawer = ["material-drawer-core", "material-drawer-iconics"]
|
material-drawer = ["material-drawer-core", "material-drawer-iconics"]
|
||||||
|
|
Loading…
Reference in New Issue