com.d4rk.androidtutorials.java
com.d4rk.androidtutorials.java copied to clipboard
Navigation Race Condition in MapsToPreferredDestination Method
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:
- Launch the app
- Quickly navigate between screens during app startup
- 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);
}