Skip to content

Instantly share code, notes, and snippets.

@ForceTower
Created August 7, 2024 14:13
Show Gist options
  • Save ForceTower/041dd15fdd39e821f782add18d0ecc90 to your computer and use it in GitHub Desktop.
Save ForceTower/041dd15fdd39e821f782add18d0ecc90 to your computer and use it in GitHub Desktop.
Safe Navigate
import android.os.Bundle
import androidx.annotation.IdRes
import androidx.navigation.NavController
import androidx.navigation.NavDestination
import androidx.navigation.NavDirections
import androidx.navigation.NavOptions
import androidx.navigation.Navigator
/**
* Navigate via the given [NavDirections]
*
* @param directions directions that describe this navigation operation
*/
fun NavController.safeNavigate(directions: NavDirections) {
safeNavigate(directions.actionId, directions.arguments)
}
/**
* Navigate via the given [NavDirections]
*
* @param directions directions that describe this navigation operation
* @param navOptions special options for this navigation operation
*/
fun NavController.safeNavigate(directions: NavDirections, navOptions: NavOptions?) {
safeNavigate(directions.actionId, directions.arguments, navOptions)
}
/**
* Navigate via the given [NavDirections]
*
* @param directions directions that describe this navigation operation
* @param navigatorExtras extras to pass to the [Navigator]
*/
fun NavController.safeNavigate(
directions: NavDirections,
navigatorExtras: Navigator.Extras
) {
safeNavigate(directions.actionId, directions.arguments, null, navigatorExtras)
}
/**
* Navigate to a destination from the current navigation graph. This supports both navigating
* via an [action][NavDestination.getAction] and directly navigating to a destination.
*
* @param resId an [action][NavDestination.getAction] id or a destination id to
* navigate to
*/
fun NavController.safeNavigate(@IdRes resId: Int) {
safeNavigate(resId, null)
}
/**
* Navigate to a destination from the current navigation graph. This supports both navigating
* via an [action][NavDestination.getAction] and directly navigating to a destination.
*
* @param resId an [action][NavDestination.getAction] id or a destination id to
* navigate to
* @param args arguments to pass to the destination
*/
fun NavController.safeNavigate(@IdRes resId: Int, args: Bundle?) {
safeNavigate(resId, args, null)
}
/**
* Navigate to a destination from the current navigation graph. This supports both navigating
* via an [action][NavDestination.getAction] and directly navigating to a destination.
*
* @param resId an [action][NavDestination.getAction] id or a destination id to
* navigate to
* @param args arguments to pass to the destination
* @param navOptions special options for this navigation operation
*/
fun NavController.safeNavigate(
@IdRes resId: Int,
args: Bundle?,
navOptions: NavOptions?
) {
safeNavigate(resId, args, navOptions, null)
}
/**
* Navigate to a destination from the current navigation graph. This supports both navigating
* via an {@link NavDestination#getAction(int) action} and directly navigating to a destination.
*
* @param resId an {@link NavDestination#getAction(int) action} id or a destination id to
* navigate to
* @param args arguments to pass to the destination
* @param navOptions special options for this navigation operation
* @param navigatorExtras extras to pass to the Navigator
*/
fun NavController.safeNavigate(
@IdRes resId: Int,
args: Bundle?,
navOptions: NavOptions?,
navigatorExtras: Navigator.Extras?
) {
if (currentDestination?.getAction(resId) != null) {
navigate(resId, args, navOptions, navigatorExtras)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment