Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bottom sheet dialog fragment navigation issues #74

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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