Skip to content

Navigation Race Condition in MapsToPreferredDestination Method #278

@Mehrad25Software

Description

@Mehrad25Software

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);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions