Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
580 views
in Technique[技术] by (71.8m points)

android maps stop responding after resuming fragment

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();
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...