Issue
I'm developing an app that shows a map on three different MapActivities.
To accomplish this I re-use a MapFragment across this three FragmentActivities which extend MapActivities using the Pete Doyle's port of the Android Compatibility package.
The MapView used in this MapFragment is kept at the Application Context.
In order to avoid the "this view already has a parent" error, I remove the view from the current parent when opening a different activity:
ViewGroup parentViewGroup = (ViewGroup) app.mapViewContainer.getParent();
if( null != parentViewGroup ) {
parentViewGroup.removeView(app.mapViewContainer);
}
It all works well until the moment when i press the phone's back button and get to a previous MapActivity. At this time, the MapView is all black, since I removed it from its parent when changing activities, and the back button doesn't trigger a re-creation of the view...
I'm aware of this post: How to use multiple MapActivities/MapViews per Android application/process
As a matter of fact, I got the idea to re-use the MapView across activities from the answer Danny Remington - MacroSolve gave.
I haven't tried to use multiple processes since I believe the solution I'm trying to implement is much lighter on resources.
Any help would be much appreciated!
Solution
Fixed my own problem...
When the MapFragment is being resumed I just had to remove all views from the fragment and from the mapview's parent, and then add the mapview to the fragment:
@Override
public void onResume() {
super.onResume();
resumed++;
if (resumed > 0) {
ViewGroup view = (ViewGroup) this.getView();
view.removeAllViews();
ViewGroup parentViewGroup = (ViewGroup) app.mapViewContainer.getParent();
if (parentViewGroup != null) {
parentViewGroup.removeAllViews();
}
view.addView(app.mapViewContainer);
}
}
Answered By - vigilnt
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.