I am getting this error with following message in different scenario:
I am writing an app where Two Activity interacting with each Other. Activity-A launch Activity-B using Intent. This Activity-B class have two fragment children. Fragment-A use public method of activity to launch another Fragment-B.
public void beginTransaction(ID id, Bundle bundle) {
Fragment fragment = getFragmentItem(id);
// In case this activity was started with special instructions from an Intent,
// pass the Intent's extras to the fragment as arguments
fragment.setArguments(bundle);
// Add the fragment to the 'fragment_container' FrameLayout
fragmentManager.beginTransaction()
.add(R.id.fragment_container, fragment).commitAllowingStateLoss();
}
Fragment-B every time call finish() to kill Activity-B hence control move back to Activity-A again. And same process repeat. After doing 2-3 time I am getting:
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState E/AndroidRuntime( 9008): at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1354) E/AndroidRuntime( 9008): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1372)
so to avoid it I follow https://stackoverflow.com/a/10261438/2624806 and it started to give me
Java.lang.IllegalStateException: Activity has been destroyed E/AndroidRuntime( 9235): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1376) E/AndroidRuntime( 9235): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
any suggestion what I am missing here.
GOT IT I got solution ..it is mistake happening in transaction for adding fragment. I was using .add instead to use .replace (as my requirement not to maintain stack) and use commitAllowTransaction to commit it. I read http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html as well and seems what I did could be costly but for normal fragment transaction mention work-around work great.
As i can not comment on your question due to less reputation point. I assume this is your public method to change fragment.
public void beginTransaction(ID id, Bundle bundle)
In this method every time you are adding fragment to activity. So if you are adding fragment first time this will work fine but in case of second fragment you should use replace not add
fragmentManager.beginTransaction()
.replace(R.id.fragment_container, fragment).commitAllowingStateLoss();
I think after doing this you should not face this problem. or you can use 'replace' for both first and second fragment. I hope this will help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With