I'm a beginner with android and i have to create a simple app that handle a lessons book for students. Basically, sometimes i log out and crash with this error, sometimes not. I don't use se function "onSaveInstanceState" but give me error when i destory a fragment with 3 segments of list of lessons. The code on "onDestroy" is necessary cause, if i change fragment,then the list desappear...with te function inside onDestry i solved it. I'm post some information, if you need more just ask...thanks.
Destroy of the fragment:
@Override
public void onDestroyView() {
super.onDestroyView();
final FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
for (Fragment fragment : adapter.getFragmentArrayList()) {
fragmentTransaction.remove(fragment);
}
fragmentTransaction.commit(); //give error here
}
Error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.progettofinale, PID: 25843
java.lang.RuntimeException: Unable to destroy activity {com.example.progettofinale/com.example.progettofinale.MainActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:5111)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:5140)
at android.app.servertransaction.DestroyActivityItem.execute(DestroyActivityItem.java:44)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at androidx.fragment.app.FragmentManager.checkStateLoss(FragmentManager.java:1551)
at androidx.fragment.app.FragmentManager.enqueueAction(FragmentManager.java:1591)
at androidx.fragment.app.BackStackRecord.commitInternal(BackStackRecord.java:341)
at androidx.fragment.app.BackStackRecord.commit(BackStackRecord.java:306)
at com.example.progettofinale.ui.mylesson.MyLessonFragment.onDestroyView(MyLessonFragment.java:62)
at androidx.fragment.app.Fragment.performDestroyView(Fragment.java:3232)
at androidx.fragment.app.FragmentStateManager.destroyFragmentView(FragmentStateManager.java:744)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:327)
at androidx.fragment.app.SpecialEffectsController$FragmentStateManagerOperation.complete(SpecialEffectsController.java:771)
at androidx.fragment.app.SpecialEffectsController$Operation.cancel(SpecialEffectsController.java:615)
at androidx.fragment.app.SpecialEffectsController.forceCompleteAllOperations(SpecialEffectsController.java:350)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2844)
at androidx.fragment.app.FragmentManager.dispatchDestroyView(FragmentManager.java:2812)
at androidx.fragment.app.Fragment.performDestroyView(Fragment.java:3225)
at androidx.fragment.app.FragmentStateManager.destroyFragmentView(FragmentStateManager.java:744)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:327)
at androidx.fragment.app.SpecialEffectsController$FragmentStateManagerOperation.complete(SpecialEffectsController.java:771)
at androidx.fragment.app.SpecialEffectsController$Operation.cancel(SpecialEffectsController.java:615)
at androidx.fragment.app.SpecialEffectsController.forceCompleteAllOperations(SpecialEffectsController.java:350)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2844)
at androidx.fragment.app.FragmentManager.dispatchDestroy(FragmentManager.java:2820)
at androidx.fragment.app.FragmentController.dispatchDestroy(FragmentController.java:345)
at androidx.fragment.app.FragmentActivity.onDestroy(FragmentActivity.java:306)
at androidx.appcompat.app.AppCompatActivity.onDestroy(AppCompatActivity.java:278)
at android.app.Activity.performDestroy(Activity.java:8239)
at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1344)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:5096)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:5140)
at android.app.servertransaction.DestroyActivityItem.execute(DestroyActivityItem.java:44)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
I faced the same issue because I used to set animation on fragmentTransaction as the following
If you're doing the same as me just delete this line and everything will work fine after that