Question

I have a SupportMapFragment added programmatically with getChildFragmentManager() in my onCreate() method.

When I reopen the app after the activity has been closed, the app seems to be rendering the old child SupportMapFragment without the markers. The old child fragment isn't interactable either.

How do I fix this lifecycle issue with SupportMapFragment? Do I need to call a specific detach method or something to that effect?

Was it helpful?

Solution

The issue was to do with the way I was handling my child fragments.

Every time the parent fragment would call onCreate the children fragments would get recreated.

I did the following to handle my child fragments, but there may be a better way:

private static final String TAG_FRAGMENT_MAP = "TagFragmentMap";

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // ...
    if (savedInstanceState == null) {
        // create the fragments for the first time
        ft.add(R.id.view_flip, new SupportMapFragment(), TAG_FRAGMENT_MAP);
        ft.commit();
    }
}

// ...

public void onViewStateRestored(Bundle savedInstanceState) {
    super.onViewStateRestored(savedInstanceState);
    mMapFragment = (SupportMapFragment)findFragmentByTag(TAG_FRAGMENT_MAP);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top