Running slightly modified example of Google Maps throws BadParcelableException in the Google Maps code. The LatLng class is parcelable but it canno
This issue as been filed on code.google.com here in case you would want to star it.
In the mean time I've manage to get around this issue by simply adding my Parcelable in to a Bundle before adding it to the final onSaveInstanceState Bundle. This works because a Bundle is a known class by the MapView's internal ClassLoader.
I've create two very small util method to do this for me. Here's the code
public static Parcelable unbundleParcelable(String key, Bundle src) {
Bundle b = src.getBundle(key);
if (b != null) {
return b.getParcelable("bundle_parcelable_util_key");
}
return null;
}
public static void bundleParcelable(String key, Bundle dest, Parcelable parcelable) {
Bundle b = new Bundle();
b.putParcelable("bundle_parcelable_util_key", parcelable);
dest.putBundle(key, b);
}
I've modified the code in one of the previous post to use my temporary solution. Here's how I use it.
@Override
public void onSaveInstanceState(Bundle outState) {
// Forward the call BEFORE adding our LatLng array, else it will crash :
_mapView.onSaveInstanceState(outState);
// Put your Parcelable in the bundle:
bundleParcelable("myLatLng", outState, new LatLng(0, 0));
}
@Override
public void onCreate(Bundle savedInstanceState) {
// Forward the call :
_mapView.onCreate(savedInstanceState);
LatLng myLatLng = null;
if(savedInstanceState != null) {
// Extract your Parcelable
myLatLng = (LatLng) unbundleParcelable("myLatLng", savedInstanceState);
}
setUpMapIfNeeded();
}
This should work for any custom Parcelable that you use in your project.