ch8n / learning-resources

0 stars 0 forks source link

Fragment illegal state Exception why? #2

Open ch8n opened 3 years ago

ch8n commented 3 years ago

Article

medium: https://proandroiddev.com/illegalstateexception-you-used-a-fragment-99816e7cf71b

Topic Covered

ch8n commented 3 years ago

As a simple rule of thumb, if your application logic permits, commit your fragment transaction in onCreate or in response to user interaction. If your fragment is added based on an async event such as a network call, things get trickier.

ch8n commented 3 years ago

The reason behind this is that the fragment’s host activity has already saved its UI state and might get destroyed anytime now. The fragment transaction that you are about to do now, will not be saved in the bundle and when the user returns to this activity, this transaction will not be restored.