简体   繁体   中英

Communication between Activity and Service to change Fragments(Login)

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM