0

I'm using SupportMapFragment and it's crashing with illegal state exception while committing. Here's the code below:

  mSupportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.branch_mp_view);
        if (mSupportMapFragment == null) {
            FragmentManager fragmentManager = getChildFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            mSupportMapFragment = SupportMapFragment.newInstance();
            fragmentTransaction.replace(R.id.branch_mp_view, mSupportMapFragment).commit();
        }

        if (mSupportMapFragment != null) {
            mSupportMapFragment.getMapAsync(this);
        }

Logs:

Fatal Exception: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
       at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1538)
       at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1556)
       at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:696)
       at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:662)
       at com.android.myapp.ui.branch_master.MyFragment.setMap(MyFragment.java:1346)
       at com.android.myapp.ui.branch_master.MyFragment.handleSuccessBranchDetail(MyFragment.java:3267)
       at com.android.myapp.ui.branch_master.MyFragment$1.onResponse$230aa0b4(MyFragment.java:200)
       at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
       at android.os.Handler.handleCallback(Handler.java:815)
       at android.os.Handler.dispatchMessage(Handler.java:104)
       at android.os.Looper.loop(Looper.java:194)
       at android.app.ActivityThread.main(ActivityThread.java:5637)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)

Pls Help, I'm not able to reproduce it but i'm seeing this crash in crashlytics appears once in a while.

Jayant Arora
  • 1,241
  • 2
  • 15
  • 24
  • Possible duplicate of [java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState](https://stackoverflow.com/questions/14177781/java-lang-illegalstateexception-can-not-perform-this-action-after-onsaveinstanc) – azizbekian Jun 02 '17 at 07:01

1 Answers1

0

It looks like you are trying to show the mapFragment after an apicall completes. In the meantime the user could have already closed the app, or gone to a different screen. When this happens your Activity or Fragment gets paused..After pausing it is not possible anymore to replace fragments. You could prevent this by checking if your ap is still in the foreground. For example when onResume is called you could set a boolean named foreground to true, and to false when onPause is called. You can now use this variable to see if your fragment can be displayed.

Rockney
  • 10,380
  • 2
  • 20
  • 26