0

So I have my MainActivity which has a BottomNavigationView, in there I have 3 different tabs which redirects me to 3 different fragments when I click them.

In FragmentA I have a RecyclerView with items, each item has a button. When I click said button I want to send that object over to FragmentB so I can add it to the ArrayList<CustomObject> and update the RecyclerView in FragmentB to display that item.

The only issue is that I don't know how to send that object over on my button click.

adapter.setOnItemRemoveListener(new RemoveItemAdapter.OnItemRemoveListener() {
    @Override
    public void onItemRemove(int position) {
        //Do I send it from here?

    }
});
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Mark Denom
  • 987
  • 1
  • 8
  • 24
  • Possible duplicate of [How to pass an object from one activity to another on Android](https://stackoverflow.com/questions/2736389/how-to-pass-an-object-from-one-activity-to-another-on-android) – Thecave3 Feb 19 '19 at 12:13
  • I'm actually using the same activity, two different Fragments – Mark Denom Feb 19 '19 at 12:16
  • It is the same, also you've a constructor for fragments so you can use that – Thecave3 Feb 22 '19 at 13:06

1 Answers1

2

First of all implements Parcelable in your Model(Object) class and then from your Fragment A just call this -

Fragment fragmentA = new FragmentGet();
Bundle bundle = new Bundle();
bundle.putParcelable("CustomObject", customObject);
fragmentA .setArguments(bundle);

Also, in Fragment B you need to get the Arguments too -

Bundle bundle = getActivity().getArguments();
if (bundle != null) {
    model = bundle.getParcelable("CustomObject");
}

Your custom object class will look like this -

public class CustomObject implements Parcelable {

    private String name;
    private String description;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

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

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

    public CustomObject() {
    }

    protected CustomObject(Parcel in) {
        this.name = in.readString();
        this.description = in.readString();
    }

    public static final Parcelable.Creator<CustomObject> CREATOR = new Parcelable.Creator<CustomObject>() {
        @Override
        public CustomObject createFromParcel(Parcel source) {
            return new CustomObject(source);
        }

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

Just call the Fragment B from your recycler view item click listener and use the above mentioned code to pass the Custom Object using Parcelable.

Hope it helps.

Salman Khan
  • 2,822
  • 5
  • 32
  • 53
  • So I would implement *Parcelable* in my MainActivity? Doesnt that require two methods? – Mark Denom Feb 19 '19 at 10:35
  • Also in `model = bundle.getParcelable("CustomObject");` what does `model` represent? – Mark Denom Feb 19 '19 at 10:36
  • You must be having a Model class(POJO) where you are having a getter setter for your custom Object. Model is your POJO class which for which you want to make ArrayList. It's basically your custom object. – Salman Khan Feb 19 '19 at 10:37
  • I still don't understand why I need to implement **Parcelable**, will I just leave the methods that interface bring empty? – Mark Denom Feb 19 '19 at 10:40
  • Share your Custom Model class, and Parcelable is used to pass object data from and activity or fragment to another and is recommended by Android itself as it's faster than Serializable and it's used for Marshelling and Un-marshelling of the object. – Salman Khan Feb 19 '19 at 10:42
  • My CustomObject class is literally just a POJO. two fields, name and description with getters and setters – Mark Denom Feb 19 '19 at 10:43
  • Yeah I got that but I will help you with the implementation of Parcelable in that class. – Salman Khan Feb 19 '19 at 10:44
  • Also, I am not sending it from the MainActivity.java, I have the button event in the FragmentA.java – Mark Denom Feb 19 '19 at 10:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/188664/discussion-between-salman-khan-and-mark-denom). – Salman Khan Feb 19 '19 at 10:48