My MapView solution in a Fragment is largely based on the examples provided by ChristophK, user1414726 and inazaruk in this SO post: mapview-in-a-fragment
When onPause() is called in the Fragment, some cleanup is done to avoid IllegalArgumentException. Code in onPause and onStop:
if (mapViewContainer != null) {
mapViewContainer.setVisibility(View.GONE);
((ViewGroup) mapViewContainer.getParent()).removeView(mapViewContainer);
mapViewContainer = null;
}
This works fine when onPause is called while swiping to other Fragments, because onCreateView(..) gets overridden in the Fragment when MapActivity is visible again, where mapViewContainer is reinstantiated.
But when I turn the screen off; onCreateView(..) does not get overridden when the screen is powered back on (onPaused and onResume calls works fine). This causes the MapView to display black, because the view is null from the removeView call in onPause, until onCreateView(..) is called again after some interaction.
How can I force onCreateView to override or in some way to add the MapView back to the Fragment when screen powers back on?
Code inside onCreateView(..):
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// This is where you specify you activity class
Intent i = new Intent(getActivity(), MapTabActivity.class);
Window w = mLocalActivityManager.startActivity("tag", i);
mapViewContainer = w.getDecorView();
mapViewContainer.setVisibility(View.VISIBLE);
mapViewContainer.setFocusableInTouchMode(true);
((ViewGroup)
mapViewContainer).setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
return mapViewContainer; }
Cheers.