Oleg Novosad
Oleg Novosad

Reputation: 2421

How can I send android object using Bundle?

I know that there is a possibility to send object that implements Serializable or Parcelable via put/get-Serializable, put/get-Parcelable using Intents and Bundles accordingly.

I need to send a Marker object from my Activity to a Fragment. I am already sending other primitive data, but got troubles with Marker object as it is not implementing any of interfaces mentioned above. I think that creating a separate class for just having one field of Marker is not a good idea.

Any ideas?

Upvotes: 0

Views: 170

Answers (1)

Simas
Simas

Reputation: 44158

As @tyczj commented, Marker does not implement Parcelable. Further more it's final so you can't subclass it.

Your best bet is for the fragment to fetch the marker from the activity directly.

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Marker marker = ((MainActivity)getActivity).getmarker();
    if (marker != null) {
        // Do something with marker object
    }

And your activity saves the marker as a instance field and creates a method for fetching it:

Marker mMarker;

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    mMarker = mMap.addMarker(new MarkerOptions().position(new LatLng(20, 100)));
    ...
}

public Marker getMarker() {
    return mMarker;
}

Upvotes: 1

Related Questions