0

I'm facing some Android Jetpack Navigation Exceptions when navigating from one fragment to another with an action. I know what problem leads to such kind of exceptions. These do not occur in normal use. But there where some traced exceptions in Crashlytics. So it seems to be sometimes a problem. Maybe with the lifecycle or something.

My general question: Is there a way or best practice to check or deal with such kind of navigation exception?

It can't be a good solution to put every navigation statement in a try - catch block.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Mirco
  • 67
  • 10

1 Answers1

1

You can use this extension to ignore when you're trying to move to a destination that you shouldn't be able to access:

fun NavController.navigateSafely(srcId: Int, actionId: Int, bundle: Bundle) {
    if (currentDestination?.id == srcId) {
        navigate(actionId, bundle)
    }
}
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428