SpeedEX505
SpeedEX505

Reputation: 2076

SupportMapFragment show for second time

I have SupportMapFragment in my own Fragment. When I show this fragment for second time it crashes.

At first I tried to add SupportMapFragment in XML but I had same problem as its described here.

So I tried to add it programmaticaly:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.map_fragment, container, false);
        isGooglePlay=isGooglePlay();
        if(isGooglePlay){
            mMapFragment = SupportMapFragment.newInstance();
            FragmentTransaction fragmentTransaction =
                         getChildFragmentManager().beginTransaction();
            fragmentTransaction.add(R.id.map_fragment_mapRoot, mMapFragment);
            fragmentTransaction.commit(); 
            map = mMapFragment.getMap();
            if (map != null){
                map.animateCamera(CameraUpdateFactory.zoomTo(100F));
            }
        }
        return view;
    } 

It crash at line where transaction is commited.

  FATAL EXCEPTION: main
  Process: com.sabatsoft.sgs14remote, PID: 23914
  java.lang.IllegalStateException: Activity has been destroyed
    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1365)
    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
    at com.sabatsoft.sgs14remote.fragment.FlightMapFragment.onCreateView(FlightMapFragment.java:45)
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5001)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)

Upvotes: 0

Views: 251

Answers (1)

AnkitSomani
AnkitSomani

Reputation: 1192

There is a similar known issue, look here

This is a bug in the nested fragments. Basically, the child FragmentManager ends up with a broken internal state when it is detached from the activity.

A short-term workaround is to add the following code in your fragment.

@Override
public void onDetach() {
super.onDetach();

try {
     Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
     childFragmentManager.setAccessible(true);
     childFragmentManager.set(this, null);
     } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
     } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}

Upvotes: 1

Related Questions