Search code examples
androidandroid-intentparcelableandroid-bundle

Pass LinkedList to another activity


I have a Linked List in one activity (A) that I want to share with another Activity (B). The list contains a username of type string and contains coordinates of type LatLng. I am also using Intent and bundle to share data between activities. I tried using Parcelable but unable to figure out how to use it. Here is the code I have:

data.java

public class data implements Parcelable{
    private LatLng coordinates;
    private String name;


    public data() {
        name = null;
        coordinates = null;
    }

    public data(String name, LatLng coordinates)
    {
        this.name = name;
        this.coordinates = coordinates;
    }

    public data(Parcel in) {
        coordinates = in.readParcelable(LatLng.class.getClassLoader());
        name = in.readString();
    }

    public static final Creator<data> CREATOR = new Creator<data>() {
        @Override
        public data createFromParcel(Parcel in) {
            return new data(in);
        }

        @Override
        public data[] newArray(int size) {
            return new data[size];
        }
    };

    public LatLng getLatLng () {
        return coordinates;
    }

    @Override
    public int describeContents() {
        return hashCode();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeParcelable(coordinates, flags);
    }
}

Activity A

public class A extends FragmentActivity implements
    OnMapReadyCallback,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    GoogleMap.OnMyLocationButtonClickListener,
    ActivityCompat.OnRequestPermissionsResultCallback {

    Button switchToSeek;
    double mLatitude;
    double mLongitude;

    LinkedList<data> storedData = new LinkedList<>();

    protected void onCreate(Bundle savedInstanceState) {
        ...
        switchToSeek.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getCurrentLocation();

                        Intent intent = new Intent(A.this, B.class);

                        Bundle xy = new Bundle();
                        xy.putDouble("x", mLatitude);
                        xy.putDouble("y", mLongitude);
                        xy.putParcelable("list", storedData); <---------- error: wrong second arugment

                        intent.putExtra("xy", xy);
                        A.this.startActivity(intent);
                    }
                });

Activity B

public class B extends FragmentActivity implements OnMapReadyCallback {

    double mLatitude;
    double mLongitude;
    LatLng current;
    GoogleMap gMap;

    LinkedList <data> copyData = new LinkedList<>();

    @Override
        public void onMapReady(GoogleMap googleMap) {
            gMap = googleMap;

            ...

            Intent intent = getIntent();
            Bundle xy = intent.getBundleExtra("xy");

            if (xy != null) {
                mLatitude = xy.getDouble("x");
                mLongitude = xy.getDouble("y");
            }
           /***** Call linked list here and set equal to copyData *****/

            current = new LatLng(mLatitude, mLongitude);
            gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(current, 18.0f));

        }

Solution

  • There is no easy way to do that, since LinkedList does not implement serializable or parcelable.

    You CAN implement your own linked list class and make it a serializable/parcelable object which can then be passed.

    Or you can convert its content into another data type such as an array and then recreate the linkedlist.* THIS IS HIGHLY INEFFICIENT

    I believe there are other ways but this is a standard problem in android dev. Maybe try using fragments if possible and passing the linkedlist through a setter()