From 842677fab4f2faa84bd290bb35145e2fe1b13983 Mon Sep 17 00:00:00 2001 From: Alexander Capehart Date: Tue, 25 Jul 2023 19:51:08 -0600 Subject: [PATCH] ui: fix broken back navigation Caused by a change in a dependency update that suddenly causes FragmentManager's back callback to to jump ahead of ours for no reason. --- .../java/org/oxycblt/auxio/MainFragment.kt | 53 ++++++------------- 1 file changed, 15 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/org/oxycblt/auxio/MainFragment.kt b/app/src/main/java/org/oxycblt/auxio/MainFragment.kt index b1183b194..2d510ea36 100644 --- a/app/src/main/java/org/oxycblt/auxio/MainFragment.kt +++ b/app/src/main/java/org/oxycblt/auxio/MainFragment.kt @@ -26,7 +26,6 @@ import androidx.activity.OnBackPressedCallback import androidx.core.view.ViewCompat import androidx.core.view.isInvisible import androidx.core.view.updatePadding -import androidx.fragment.app.FragmentContainerView import androidx.fragment.app.activityViewModels import androidx.navigation.NavController import androidx.navigation.NavDestination @@ -78,7 +77,6 @@ class MainFragment : private var sheetBackCallback: SheetBackPressedCallback? = null private var detailBackCallback: DetailBackPressedCallback? = null private var selectionBackCallback: SelectionBackPressedCallback? = null - private var exploreBackCallback: ExploreBackPressedCallback? = null private var lastInsets: WindowInsets? = null private var elevationNormal = 0f private var initialNavDestinationChange = true @@ -104,28 +102,17 @@ class MainFragment : // Currently all back press callbacks are handled in MainFragment, as it's not guaranteed // that instantiating these callbacks in their respective fragments would result in the // correct order. - val sheetBackCallback = + sheetBackCallback = SheetBackPressedCallback( - playbackSheetBehavior = playbackSheetBehavior, - queueSheetBehavior = queueSheetBehavior) - .also { sheetBackCallback = it } + playbackSheetBehavior = playbackSheetBehavior, + queueSheetBehavior = queueSheetBehavior) val detailBackCallback = DetailBackPressedCallback(detailModel).also { detailBackCallback = it } val selectionBackCallback = SelectionBackPressedCallback(listModel).also { selectionBackCallback = it } - val exploreBackCallback = - ExploreBackPressedCallback(binding.exploreNavHost).also { exploreBackCallback = it } // --- UI SETUP --- val context = requireActivity() - // Override the back pressed listener so we can map back navigation to collapsing - // navigation, navigation out of detail views, etc. - context.onBackPressedDispatcher.apply { - addCallback(viewLifecycleOwner, exploreBackCallback) - addCallback(viewLifecycleOwner, selectionBackCallback) - addCallback(viewLifecycleOwner, detailBackCallback) - addCallback(viewLifecycleOwner, sheetBackCallback) - } binding.root.setOnApplyWindowInsetsListener { _, insets -> lastInsets = insets @@ -182,6 +169,18 @@ class MainFragment : binding.playbackSheet.viewTreeObserver.addOnPreDrawListener(this@MainFragment) } + override fun onResume() { + super.onResume() + // Override the back pressed listener so we can map back navigation to collapsing + // navigation, navigation out of detail views, etc. We have to do this here in + // onResume or otherwise the FragmentManager will have precedence. + requireActivity().onBackPressedDispatcher.apply { + addCallback(viewLifecycleOwner, requireNotNull(selectionBackCallback)) + addCallback(viewLifecycleOwner, requireNotNull(detailBackCallback)) + addCallback(viewLifecycleOwner, requireNotNull(sheetBackCallback)) + } + } + override fun onStop() { super.onStop() val binding = requireBinding() @@ -194,7 +193,6 @@ class MainFragment : sheetBackCallback = null detailBackCallback = null selectionBackCallback = null - exploreBackCallback = null } override fun onPreDraw(): Boolean { @@ -296,8 +294,6 @@ class MainFragment : // Drop the initial call by NavController that simply provides us with the current // destination. This would cause the selection state to be lost every time the device // rotates. - requireNotNull(exploreBackCallback) { "ExploreBackPressedCallback was not available" } - .invalidateEnabled() if (!initialNavDestinationChange) { initialNavDestinationChange = true return @@ -486,23 +482,4 @@ class MainFragment : isEnabled = selection.isNotEmpty() } } - - private inner class ExploreBackPressedCallback( - private val exploreNavHost: FragmentContainerView - ) : OnBackPressedCallback(false) { - // Note: We cannot cache the NavController in a variable since it's current destination - // value goes stale for some reason. - - override fun handleOnBackPressed() { - exploreNavHost.findNavController().navigateUp() - logD("Forwarded back navigation to explore nav host") - } - - fun invalidateEnabled() { - val exploreNavController = exploreNavHost.findNavController() - isEnabled = - exploreNavController.currentDestination?.id != - exploreNavController.graph.startDestinationId - } - } }