我似乎无法理解这一个。我已经完成了Stack Overflow提供的可能解决方案。
我有2个活动,比如A和B;当我从B回到A然后选择一个图像回到B时,没有任何片段事务工作并抛出错误,如下所示。
代码:
MainMenuFragment.java
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.menu_filter:
activity.changeMode(EditImageActivity.MODE_FILTERS);
activity.sliderFragment.resetBitmaps();
activity.changeMiddleFragment(EditImageActivity.MODE_FILTERS);
break;
case R.id.menu_enhance:
activity.changeMode(EditImageActivity.MODE_ENHANCE);
activity.sliderFragment.resetBitmaps();
activity.changeMiddleFragment(EditImageActivity.MODE_ENHANCE);
break;
case R.id.menu_adjust:
activity.changeMode(EditImageActivity.MODE_ADJUST);
activity.changeMiddleFragment(EditImageActivity.MODE_ADJUST);
break;
case R.id.menu_sticker:
activity.changeMode(EditImageActivity.MODE_STICKER_TYPES);
activity.changeMiddleFragment(EditImageActivity.MODE_STICKER_TYPES);
break;
case R.id.menu_write:
activity.changeMode(EditImageActivity.MODE_WRITE);
activity.changeMiddleFragment(EditImageActivity.MODE_WRITE);
break;
}
}
EditImageActivity.java
public void changeMiddleFragment(int index) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.slide_up, R.anim.slide_down);
transaction.replace(R.id.preview_container, getFragment(index, this));
transaction.commit();
}
错误:
FATAL EXCEPTION: main
Process: org.ArtIQ.Rex, PID: 9739
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1842)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1860)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:650)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:609)
at org.ArtIQ.Rex.editor.EditImageActivity.changeMiddleFragment(EditImageActivity.java:392)
at org.ArtIQ.Rex.editor.fragment.MainMenuFragment.onClick(MainMenuFragment.java:82)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
答案 0 :(得分:0)
将transaction.commit()
更改为transaction.commitAllowingStateLoss()
可解决此问题。