I am very new to java and android. I am stuck with a basic problem.
In this given fragment
, I can add GoogleMap
object as parcelable
without any extra pracelable
class as:
public class SecondFragment extends Fragment
implements OnMapReadyCallback {
public GoogleMap mMap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
mLastKnownLocation = savedInstanceState.getParcelable(KEY_LOCATION);
mCameraPosition = savedInstanceState.getParcelable(KEY_CAMERA_POSITION);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
if (mMap != null) {
outState.putParcelable(KEY_CAMERA_POSITION, mMap.getCameraPosition());
outState.putParcelable(KEY_LOCATION, mLastKnownLocation);
super.onSaveInstanceState(outState);
}
}
This is my first encounter with this parcelable approach, so, I tried the same in another class for a string:
public String location;//="No location name found";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Retrieve location and camera position from saved instance state.
if (savedInstanceState != null) {
location = savedInstanceState.getParcelable(KEY_LOCATION_NAME);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
if (location != null) {
outState.putParcelable(KEY_LOCATION_NAME, location);
super.onSaveInstanceState(outState);
}
}
This shows compilation time error:
Error:(205, 49) error: incompatible types: String cannot be converted to Parcelable
So, my question is why the same thing is working for googleMap and not for a string? Is googleMap inherently parcelable, and for "normal" String, we must create a parcelable class?