Restoring MapView's state on rotate and on back

前端 未结 3 1467
Happy的楠姐
Happy的楠姐 2020-12-07 14:35

Background

I have a larger application in which I had/have several problems with new Google Maps API. I tried to describe it in a different question

相关标签:
3条回答
  • 2020-12-07 15:11

    Implements your fragment/activity with GoogleMap.OnCameraChangeListener and override method onCameraChange and into you can save the new camera position, and onResume use

    googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPositionSaved));
    
    0 讨论(0)
  • 2020-12-07 15:16

    Here is my fix to the first problem:

    It seems that Map is trying to unparcel all the bundle, not just it's own information when I call mMap.onCreate(savedInstanceState) and it has problem with it if I'm using my custom Parcelable class. The solution that worked for me was removing my extras from savedInstanceState as soon as I used them - before I call Map's onCreate(). I do it with savedInstanceState.remove(MY_KEY). Another thing I had to do was to call mMap.onSaveInstanceState() before adding my own information to outState in Fragment's onSaveInstanceState(Bundle outState) function.

    And here's how I handled the second one:

    I simplified the example project to the bare bones. I was adding raw Markers to the map and if I replace the Fragment with map with another one then after clicking "back" I still got "nulled" map. So I did two things:

    1. saving CameraPosition in onPause() function to restore it in onResume()
    2. setting mMap to null in onPause() so when the Fragment comes back, the Markers are added again by the addMapPoints() function (I had to change it a little bit since I was saving and checking Markers id's).

    Here are code samples:

    private CameraPosition cp;
    

    ...

    public void onPause() {
        mMapView.onPause();
        super.onPause();
    
        cp = mMap.getCameraPosition();
        mMap = null;
    }
    

    ...

    public void onResume() {
        super.onResume();
        setUpMapIfNeeded();
        mMapView.onResume();
        if (cp != null) {
            mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cp));
            cp = null;
        }
    }
    

    And to update the camera position in onResume() I had to manually initialize maps. I did it in setUpMap():

    private void setUpMap() {
        try {
            MapsInitializer.initialize(getActivity());
        } catch (GooglePlayServicesNotAvailableException e) {
        }
        mMap.setOnInfoWindowClickListener(this);
        mMap.setOnMapLongClickListener(this);
        addMapPoints();
    }
    

    I realize that those aren't real solutions - just overrides but it's the best I can do for now and the project must go on. If anyone finds cleaner fixes I'll be grateful for letting me know about them.

    0 讨论(0)
  • 2020-12-07 15:26

    Came here in search of any hints regarding onSaveInstance and NullPointerException. I have tried various workarounds including the one mentioned by @Izydorr but have had no luck. The issue was with FragmentPagerAdapter - the adapter of the ViewPager that was hosting my fragments that were embedding the MapViews. After having it changed to FragmentStatePagerAdapter the NPEs have gone away. Whew!

    0 讨论(0)
提交回复
热议问题