com.d4rk.androidtutorials.java icon indicating copy to clipboard operation
com.d4rk.androidtutorials.java copied to clipboard

Navigation Race Condition in MapsToPreferredDestination Method

Open Mehrad25Software opened this issue 3 months ago • 0 comments

Method: navigateToPreferredDestination() in MainActivity.java

Description: A race condition occurs when the navigateToPreferredDestination() method is called before the fragment is fully attached to the activity. This causes an IllegalStateException and app crash.

Steps to Reproduce:

  1. Launch the app
  2. Quickly navigate between screens during app startup
  3. Call navigateToPreferredDestination() before fragment lifecycle completes

Expected Behavior: Navigation should wait for fragment to be fully attached or handle gracefully.

Actual Behavior: App crashes with IllegalStateException: Fragment not attached to activity

Proposed Fixed Add this condition right before navigation:


if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)) {
    navController.navigate(preferredDestination, null, options);
}

Mehrad25Software avatar Nov 11 '25 03:11 Mehrad25Software