Last active
November 29, 2022 07:50
-
-
Save muhammad-hamza-shahid/28548de0b0cbbf1e0971b46ba06f717d to your computer and use it in GitHub Desktop.
Safe navigation using Navigation Component Jetpack Android (DeepLink, Action, Fragment Name)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.net.Uri | |
import android.os.Bundle | |
import android.util.Log | |
import androidx.annotation.IdRes | |
import androidx.fragment.app.Fragment | |
import androidx.navigation.NavDirections | |
import androidx.navigation.fragment.DialogFragmentNavigator | |
import androidx.navigation.fragment.FragmentNavigator | |
import androidx.navigation.fragment.findNavController | |
// Safe navigation to avoid Fragment not found due to double click or delay in navigation | |
// Pass direction where you want to navigate | |
fun Fragment.safeNavigateFromNavController(directions: NavDirections, bundle: Bundle? = null) { | |
val navController = findNavController() | |
when (val destination = navController.currentDestination) { | |
is FragmentNavigator.Destination -> { | |
if (javaClass.name == destination.className) { | |
if (bundle == null) { | |
navController.navigate(directions) | |
} else { | |
// navController.navigate(directions, bundle) | |
} | |
} else { | |
Log.e("FsafeNavigateFrom", "doubleClicked") | |
} | |
} | |
is DialogFragmentNavigator.Destination -> { | |
if (javaClass.name == destination.className) { | |
if (bundle == null) { | |
navController.navigate(directions) | |
} else { | |
// navController.navigate(directions, bundle) | |
} | |
} else { | |
Log.e("DsafeNavigateFrom", "doubleClicked") | |
} | |
} | |
} | |
} | |
// Pass deeplink uri where you want to navigate | |
fun Fragment.safeNavigateFromNavController(directions: Uri, bundle: Bundle? = null) { | |
val navController = findNavController() | |
when (val destination = navController.currentDestination) { | |
is FragmentNavigator.Destination -> { | |
if (javaClass.name == destination.className) { | |
if (bundle == null) { | |
navController.navigate(directions) | |
} else { | |
// navController.navigate(directions, bundle) | |
} | |
} else { | |
Log.e("FsafeNavigateFrom", "doubleClicked") | |
} | |
} | |
is DialogFragmentNavigator.Destination -> { | |
if (javaClass.name == destination.className) { | |
if (bundle == null) { | |
navController.navigate(directions) | |
} else { | |
// navController.navigate(directions, bundle) | |
} | |
} else { | |
Log.e("DsafeNavigateFrom", "doubleClicked") | |
} | |
} | |
} | |
} | |
// Pass fragment id where you want to navigate | |
fun Fragment.safeNavigateFromNavController(@IdRes directions: Int, bundle: Bundle? = null) { | |
val navController = findNavController() | |
when (val destination = navController.currentDestination) { | |
is FragmentNavigator.Destination -> { | |
if (javaClass.name == destination.className) { | |
if (bundle == null) { | |
navController.navigate(directions) | |
} else { | |
// navController.navigate(directions, bundle) | |
} | |
} else { | |
Log.e("FsafeNavigateFrom", "doubleClicked") | |
} | |
} | |
is DialogFragmentNavigator.Destination -> { | |
if (javaClass.name == destination.className) { | |
if (bundle == null) { | |
navController.navigate(directions) | |
} else { | |
// navController.navigate(directions, bundle) | |
} | |
} else { | |
Log.e("DsafeNavigateFrom", "doubleClicked") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment