Android : Android fragment transition happens after app enters background

on Wednesday, October 29, 2014


I have a registration activity, inside which I have multiple fragments (step-by-step registration flow)..


In the activity, I have this updateRegistrationStage() method that handles fragment transition.. here is the problem: 1) I click continue button, submit some data to server and waiting for response.. 2) Before I get any response, I press home button so app enters background.. 3) then, I got the response and supposed to jump to next fragment... at this point, I call:



transaction = fm.beginTransaction();
transaction.add(R.id.registration_container, newFragment, newFragment.toString());
transaction.commit();


And app crashes.. throwing



java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState


if I change last line to



transaction.commitAllowingStateLoss();


It would say



java.lang.IllegalStateException: Activity has been destroyed


I think it's possible to set a flag e.g. isActivityAlive to make sure transaction commit when activity is still alive... but.. that's pretty ugly. Is there a better way of cancelling fragment transaction when the activity is no longer active


0 comments:

Post a Comment