简体   繁体   中英

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?

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM