Reputation: 8177
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.
Upvotes: 0
Views: 1177
Reputation: 26
I might have found an answer to our problem. I don't think it's a good one, but it seems to work..
If you save a reference to the mapViewContainer's parent (I made it static) in onStart() you can add the mapViewContainer to the parent in onResume() if it's not already contained.
Here is what I did:
private static ViewParent parent;
@Override
public void onStart() {
super.onStart();
parent = mapViewContainer.getParent();
}
@Override
public void onResume() {
super.onResume();
if(mapViewContainer != null && parent != null && ((FrameLayout) parent).findViewById(mapViewContainer.getId()) == null) {
((FrameLayout) parent).addView(mapViewContainer);
}
}
In onPause() I do not set mapViewContainer == null, so I can reuse it.
If you have any suggestion to improve this 'solution' you're very welcome!
Hope it helps.
CHEERS
Upvotes: 1