Skip to content

Instantly share code, notes, and snippets.

@Shipaaaa
Created November 4, 2020 15:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shipaaaa/5f979f1c04b3f1a118f943a33b072794 to your computer and use it in GitHub Desktop.
Save Shipaaaa/5f979f1c04b3f1a118f943a33b072794 to your computer and use it in GitHub Desktop.
package ru.shipa.app.extension
import android.os.Bundle
import androidx.annotation.IdRes
import androidx.navigation.NavController
import androidx.navigation.NavDirections
import androidx.navigation.NavOptions
import androidx.navigation.Navigator
/**
* Позволяет осуществлять переход по навигации в безопасном режиме и
* предотвращает краш при открытии более чем одного экрана (мультитач нажатии).
*/
fun NavController.navigateSafe(direction: NavDirections) {
currentDestination?.getAction(direction.actionId)?.let { navigate(direction) }
}
/**
* Позволяет осуществлять переход по навигации в безопасном режиме и
* предотвращает краш при открытии более чем одного экрана (мультитач нажатии).
*/
fun NavController.navigateSafe(
@IdRes resId: Int,
args: Bundle? = null,
navOptions: NavOptions? = null,
navExtras: Navigator.Extras? = null
) {
val action = currentDestination?.getAction(resId) ?: graph.getAction(resId)
if (action != null && currentDestination?.id != action.destinationId) {
navigate(resId, args, navOptions, navExtras)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment