Skip to content

Commit

Permalink
Fix dialog fragment navigation issues. This fixes regressions when re…
Browse files Browse the repository at this point in the history
…factoring the Navigator to use a single instance across a navigation host. The topmost DialogFragment is not available from the NavigatorHost.childFragmentManager, since it is added directly to the Activity's window instead.
  • Loading branch information
jayohms committed Dec 2, 2024
1 parent 71f8cdc commit 8999d02
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ abstract class HotwireBottomSheetFragment : BottomSheetDialogFragment(),

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
navigator.currentDialogDestination = this
delegate.onViewCreated()

if (shouldObserveTitleChanges()) {
Expand All @@ -43,6 +44,11 @@ abstract class HotwireBottomSheetFragment : BottomSheetDialogFragment(),
}
}

override fun onDestroyView() {
navigator.currentDialogDestination = null
super.onDestroyView()
}

/**
* This is marked `final` to prevent further use, as it's now deprecated in
* AndroidX's Fragment implementation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ class Navigator(
private val navController = host.navController

/**
* Retrieves the currently active [HotwireDestination] on the backstack.
* The currently active dialog destination on the backstack if present, otherwise null.
* Dialog fragments are added to the Activity window and are not added directly to the
* [NavigatorHost] childFragmentManager.
*/
internal var currentDialogDestination: HotwireDialogDestination? = null

/**
* Retrieves the currently active (fullscreen) [HotwireDestination] on the backstack.
*/
val currentDestination: HotwireDestination
get() = host.childFragmentManager.primaryNavigationFragment as HotwireDestination?
Expand Down Expand Up @@ -85,9 +92,9 @@ class Navigator(
*/
fun pop() {
navigateWhenReady {
val currentFragment = currentDestination.fragment
if (currentFragment is HotwireDialogDestination) {
currentFragment.closeDialog()
val currentDialogDestination = currentDialogDestination
if (currentDialogDestination != null) {
currentDialogDestination.closeDialog()
} else {
navController.popBackStack()
}
Expand Down Expand Up @@ -162,10 +169,8 @@ class Navigator(
}

navigateWhenReady {
val currentFragment = currentDestination.fragment
if (currentFragment is HotwireDialogDestination) {
currentFragment.closeDialog()
}
// If a dialog is on top of the backstack, close it first
currentDialogDestination?.closeDialog()

navController.popBackStack(navController.graph.startDestinationId, false)
onCleared()
Expand Down Expand Up @@ -264,7 +269,7 @@ class Navigator(
)

navigateWhenReady {
val isDialog = currentDestination.fragment is HotwireDialogDestination
val isDialog = currentDialogDestination != null
if (isDialog) {
// Pop the backstack before sending the modal result, since the
// underlying fragment is still active and will receive the
Expand Down

0 comments on commit 8999d02

Please sign in to comment.