Skip to content

Commit

Permalink
Merge pull request #74 from hotwired/dialog-fixes
Browse files Browse the repository at this point in the history
Fix bottom sheet dialog fragment navigation issues
  • Loading branch information
jayohms authored Dec 2, 2024
2 parents 71f8cdc + 8999d02 commit a103ddc
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 a103ddc

Please sign in to comment.