Is GoogleMap inherently Parcelable?

2019-07-14 04:58发布

问题:

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?

回答1:

You can read the GoogleMap class page to see that it does not implements Parcleable.

the same thing is working for googleMap

You are not saving the map... You are saving the CameraPosition

location is a String, not a Google Map.

If you want to save the location, a LatLng or Location is Parcelable.

for "normal" String, we must create a parcelable class?

Nope. With a string, just use outState.putString()



回答2:

Parcelable is used for complex models.

If you want to convert a model to parcelable you should implement Parcelable . There are some plugins which do all this hard job for you in Android studio. In your example, pay attention that passing object in intents or saving them to instance to use later have different methods for each type.

putString for string
putInt for Int
....

for your desired type you should use the appropriate method



回答3:

String is base type, so you should not use it for Parcelable.

If you want to use Parcelable for a class object. the class should implement Parcelable already.