diff --git a/navigation-fragments/src/main/java/dev/hotwire/navigation/activities/HotwireActivityDelegate.kt b/navigation-fragments/src/main/java/dev/hotwire/navigation/activities/HotwireActivityDelegate.kt index 281178f..1240cc1 100644 --- a/navigation-fragments/src/main/java/dev/hotwire/navigation/activities/HotwireActivityDelegate.kt +++ b/navigation-fragments/src/main/java/dev/hotwire/navigation/activities/HotwireActivityDelegate.kt @@ -44,16 +44,17 @@ class HotwireActivityDelegate(val activity: HotwireActivity) { /** * Get the Activity's currently active [Navigator]. + * + * Returns null if the navigator is not ready for navigation. */ val currentNavigator: Navigator? - get() { - return if (currentNavigatorHost.isAdded && !currentNavigatorHost.isDetached) { - currentNavigatorHost.navigator - } else { - null - } + get() = if (currentNavigatorHost.isReady()) { + currentNavigatorHost.navigator + } else { + null } + /** * Sets the currently active navigator in your Activity. If you use multiple * [NavigatorHost] instances in your app (such as for bottom tabs), @@ -107,7 +108,7 @@ class HotwireActivityDelegate(val activity: HotwireActivity) { } private fun updateOnBackPressedCallback(navController: NavController) { - if (navController == currentNavigatorHost.navController) { + if (navController == currentNavigatorHost.navController) { onBackPressedCallback.isEnabled = navController.previousBackStackEntry != null } } diff --git a/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebBottomSheetFragment.kt b/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebBottomSheetFragment.kt index e4e8f79..ac330d8 100644 --- a/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebBottomSheetFragment.kt +++ b/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebBottomSheetFragment.kt @@ -108,7 +108,7 @@ open class HotwireWebBottomSheetFragment : HotwireBottomSheetFragment(), Hotwire /** * Gets the HotwireView instance in the Fragment's view - * with resource ID R.id.turbo_view. + * with resource ID R.id.hotwire_view. */ final override val hotwireView: HotwireView? get() = view?.findViewById(R.id.hotwire_view) diff --git a/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebFragment.kt b/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebFragment.kt index 220fd16..de0c9a5 100644 --- a/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebFragment.kt +++ b/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebFragment.kt @@ -126,7 +126,7 @@ open class HotwireWebFragment : HotwireFragment(), HotwireWebFragmentCallback { /** * Gets the HotwireView instance in the Fragment's view - * with resource ID R.id.turbo_view. + * with resource ID R.id.hotwire_view. */ final override val hotwireView: HotwireView? get() = view?.findViewById(R.id.hotwire_view) diff --git a/navigation-fragments/src/main/java/dev/hotwire/navigation/navigator/Navigator.kt b/navigation-fragments/src/main/java/dev/hotwire/navigation/navigator/Navigator.kt index f153e87..daa7f66 100644 --- a/navigation-fragments/src/main/java/dev/hotwire/navigation/navigator/Navigator.kt +++ b/navigation-fragments/src/main/java/dev/hotwire/navigation/navigator/Navigator.kt @@ -64,6 +64,18 @@ class Navigator( } } + /** + * Returns whether the navigator and its host are ready for navigation. It is not + * ready for navigation if the host view is not attached or the start destination + * has not been created yet. + */ + fun isReady(): Boolean { + return host.isReady() + } + + /** + * Returns whether the current destination is the only backstack entry. + */ fun isAtStartDestination(): Boolean { return navController.previousBackStackEntry == null } diff --git a/navigation-fragments/src/main/java/dev/hotwire/navigation/navigator/NavigatorHost.kt b/navigation-fragments/src/main/java/dev/hotwire/navigation/navigator/NavigatorHost.kt index b5a2273..782f8b1 100644 --- a/navigation-fragments/src/main/java/dev/hotwire/navigation/navigator/NavigatorHost.kt +++ b/navigation-fragments/src/main/java/dev/hotwire/navigation/navigator/NavigatorHost.kt @@ -27,6 +27,15 @@ open class NavigatorHost : NavHostFragment() { activity.delegate.unregisterNavigatorHost(this) } + /** + * Returns whether the navigation host is ready for navigation. It is not + * ready for navigation if the view is not attached or the start destination + * has not been created yet. + */ + fun isReady(): Boolean { + return isAdded && !isDetached && childFragmentManager.primaryNavigationFragment != null + } + internal fun initControllerGraph() { navController.apply { graph = NavigatorGraphBuilder(