android maps stop responding after resuming fragme

2019-01-28 11:53发布

问题:

Listed below is my basic code for controlling the maps. I do some really advanced stuff later. Everything seems to work perfect, until onResume().

Here is the layout, you navigate through the app in 1 single activity, with multiple fragments. This mapFragment is contained inside of a fragment. This works fine. However when I add another fragment and push this one on the back stack, when i come back to it later, the map is unresponsive.

I tried fixing this by moving my call to setupMaps(); into the onResume(), however this caused gMaps to be null when I get it from gMaps = mapFragment.getMap(); in the setViews().

How should I handle this?

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        root = inflater.inflate(R.layout.fragment_maps, container, false);

        setupMaps();
        return root;
    }

    private void setupMaps()
    {
        gMaps = null;
        fm = getActivity().getSupportFragmentManager();

        mapFragment = SupportMapFragment.newInstance();
        android.support.v4.app.FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.add(R.id.flMapContainer, mapFragment).commit();

    }


    @Override
    public void onResume()
    {
        super.onResume();

        mapFragment.onResume();
        setViews();
    }

    private void setViews()
    {
        gMaps = mapFragment.getMap();
        getData(); // initializes overlays, markers, polygons etc.
    }

    @Override
    public void onPause()
    {           
        mapFragment.onPause();
        super.onPause();
    }

回答1:

Do you see anything in your logcat? I've had some issues like this before, and I believe it was related to the old map fragment's View not being removed from its parent ViewGroup before creating a new instance of it. This resulted in errors regarding a duplicate fragment.

Try removing all views from your flMapContainer before you create the new instance of the SupportMapFragment.



回答2:

To implement Scott Stanchfield's solution: call cleanFrame() when add/replace another fragment.

public void cleanFrame(){
FrameLayout FL = (FrameLayout) thisview.findViewById(R.id.myfragmentcontainer);
   FL.removeAllViewsInLayout();
}


回答3:

I have disabled hardware acceleration inside manifest.xml and after it everything started to work successfully:

<application android:hardwareAccelerated="false">
 ...
</application>