1

I'm on the way of developing application using Google Map API, I draw the path of two points (GeoPoint) successful.

And when changing from portrait to landscape, The map will invalidate (because of activity life-cycle), you know? So, I lost two that points.

I know we must save two that points in method onSavedInstanceState(Bundle outState). Therefore, I want to save two that point (GeoPoint) for draw again when changing from portrait to landscape.

I don't know how to put GeoPoint into that Bundle in that method. Please tell me?

Regards!

j0k
  • 22,600
  • 28
  • 79
  • 90
Alex Tran
  • 13
  • 4

1 Answers1

0

the API docs tells me Geopoint has a constuctor with two params: latitude, longitude. Both attributes have getters. So either you obtain those and put them in the Bundle, or you extend the class and make it parcellable, or make a wrapper class. Either solution will work just fine.

Try this, I suspect it'll work just fine.

public class ParcelableGeoPoint implements Parcelable {
    private GeoPoint point;    

    public ParcelableGeoPoint(int lat, int lon){
        point = new GeoPoint(lat, lon);
    }

    public ParcelableGeoPoint(Parcel in){
        int[] data = new int[2];
        in.readIntArray(data);
        point = new GeoPoint(data[0], data[1]);
    }

    public int describeContents(){
        return 0;
    }

    public GeoPoint unwrap() {
        return point;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeIntArray(new int[] {point.getLat(), point.getLon()});
    }
    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public ParcelableGeoPoint createFromParcel(Parcel in) {
            return new ParcelableGeoPoint(in); 
        }

        public ParcelableGeoPoint[] newArray(int size) {
            return new ParcelableGeoPoint[size];
        }
    };
}

Then, put it in your Bundle with bundle.putParcelable("MYPOINT", geopointParcelable)

Community
  • 1
  • 1
stealthjong
  • 10,858
  • 13
  • 45
  • 84