How to reuse SupportMapFragment inside another fragment

I just switched to the latest version of android extension cards (2.2.0) along with the latest Play Services (6.5.87) and library support (21.0.3). And now I cannot reuse my MapFragment.

I have MainActivity with NavigationDrawer. One of the fragments is a fragment with a GoogleMap fragment inside. When I switch between fragments in NavigationDrawer, they recreate themselves. I used to use a very simple solution for this. I used the already bloated view:

 public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    if (view != null) {
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }
        return view;
    }
    view = inflater.inflate(R.layout.fragment_map, container, false);

    SupportMapFragment mapFragment = SupportMapFragment.newInstance();
    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    transaction.add(R.id.container, mapFragment).commit();

    mapFragment.getExtendedMapAsync(new OnMapReadyCallback() {
        public void onMapReady(GoogleMap googleMap) {
            map = googleMap;
            setupMap();
        }
    });

    return view;
}

But now it will not work. The map is not displayed when this fragment is opened a second time.

I can just throw away this ugly code

 if (view != null) {
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }
        return view;
    }

. (, ), .

, -, Google Maps . , , ?

+4
1

MapFragment MapView, androidmapsextensions. .

MapView mapView.onCreate(bundle)

private MapView mapView;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    View view = inflater.inflate(R.layout.fragment_map, container, false);

    if (mapView != null) {        
        ViewGroup parentViewGroup = (ViewGroup) mapView.getParent();
        if (parentViewGroup != null) {
            parentViewGroup.removeView(mapView);            
        }
    } else {
        mapView = new MapView(getActivity());
        mapView.onCreate(Bundle.EMPTY); //need if programmatically add 
    }    

    ((ViewGroup)view.findViewById(R.id.container)).addView(mapView);

    mapView.getExtendedMapAsync(new OnMapReadyCallback() {
        public void onMapReady(GoogleMap googleMap) {
            setUpMap(GoogleMap);
        }
    });    
}    

, mapView

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    View view = inflater.inflate(R.layout.fragment_map, container, false);

    boolean needSetupMap = true;
    if (mapView != null) {        
        ViewGroup parentViewGroup = (ViewGroup) mapView.getParent();
        if (parentViewGroup != null) {
            parentViewGroup.removeView(mapView);
            needSetupMap = false;
        }
    } else {
        mapView = new MapView(getActivity());
        mapView.onCreate(Bundle.EMPTY);
    }  

    ((ViewGroup)view.findViewById(R.id.container)).addView(mapView);

    if (needSetupMap) {
        mapView.getExtendedMapAsync(new OnMapReadyCallback() {
            public void onMapReady(GoogleMap googleMap) {
                setUpMap(GoogleMap);
            }
        });
    }
}  
+3

All Articles