Communication between Activity and Service to change Fragments(Login)

242 Views Asked by At

I have LoginActivity that displays the Login Fragment initially . Now when the user fill the data and press login button I send the credentials to an IntentService . before the service hit the server it sends a local broad cast intent to the LoginActivity to display the Loading Fragment (replace the login fragment) . If the login process successfully terminated it send a broadcast intent to the LoginActivity to start the MainActivity , and if it fails it send a broadcast intent to the LoginActivity to display the Error Fragment .

Now I am doing things the right way ? what about having this error

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
    at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1341)
    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1352)
    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)

? I thing this error will happened , now how can i handle the FragmentTransaction in the onReceive() method where i change Fragments in the Login Activity.

is there a better architecture to handle the login using Activity and IntentService?

1

There are 1 best solutions below

1
On

Try to use commitAllowingStateLoss() instead of commit().

Edited:

The problem is that you call commit after the onSavedInstanceState method. You have to unregister the reciever when the activity is not in the front (in onPause())Check out this comment stackoverflow.com/a/18306939/1333170