I have an app with a few activities, but in each activity I've got some fragments. The layout of the activity has a RelativeLayout as a Fragment Container. I have added tabs navigation with ActionBarSherlock and a tab is to watch a map.
I have used MapViewBalloons library. When I click in a tab I replace the fragment with the corresponding fragment.
In the Map Fragment I have used a static variable to save my mapView object not to create a new one each time I access to this fragment.
I can enter to the map fragment, move it there, touch and move the map. Then change to another fragment with a tab, WITHOUT changing the activity, and I still have the same map, the same situation and I can navigate there.
THE PROBLEM: If I press back button, if I change orientation (I guess, if the activity is destroyed and re-created) I can enter again to the map, but if I try to move there it crashes and I've got this error:
Unable to add Window, token android.view.ViewRootImpl$... is not valid, is your activity running?
static TapControlledMapView mapView;
onCreateView...
if (mapView == null) {
//do things
else
parentViewGroup.removeView(mapView);
Button botonFiltro = (Button) parentViewGroup
.findViewById(R.id.botonFiltro);
parentViewGroup.removeView(botonFiltro);
inf.addView(mapView);
return inf;
That's how I handled the "you are only allowed to have a single map" stuff.
So... Why crashes if I change activity? I have checked that parentViewGroup is not null, so MapView have a parent (I don't know if this have any sense).
My TabListener is like that;
public class MyTabListener implements TabListener {
public Fragment fragment;
public MyTabListener(Fragment fragment) {
super();
this.fragment = fragment;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
//TODO: onTabReselected
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragmentContainer, fragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
The strangest thing is that if I don't touch the map the first time I get in, or the second time, it doesn't crash.
I'm new in Android so maybe I'm not doing things correct. Can anybody help me?
EDIT: I have used another library to make compatible ABS, Fragments and the Map. I have followed this blog: http://xrigau.wordpress.com/2012/03/22/howto-actionbarsherlock-mapfragment-listfragment/
FIXED! The problem was the one that I talkted about in the comment. The listener was attached to the old activity. Add new listener and done!