0

I have class ITEM and I want to pass array (Item[] items) of this class from ACTIVITY to FRAGMENT. Activity and fragment use Android Annotations.

public class Item implements Parcelable {

    public int id;
    public String code;

    public Item() {
    }

    public Item(int id, String code) {
        this.id = id;
        this.code = code;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    protected Item(Parcel in) {
        id = in.readInt();
        code = in.readString();
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeString(code);
    }

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

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

}

Now I do it old way - I do not use parcelable, because I do not have to, but it seems that I can´t pass same argument to fragment with Android Annotations (I may be wrong).

Activity:

new ItemFragment(items)

Fragment:

public class ItemFragment extends Fragment {

    public ItemFragment (Item[] items) {
        this.items= items;
    }

}
Michalsx
  • 3,446
  • 5
  • 33
  • 46
  • look here how to pass parcel https://www.codota.com/android/methods/android.os.Bundle/putParcelable And https://github.com/johncarl81/parceler for more info – K.Sopheak Feb 01 '17 at 09:35
  • Take a look. Might be helpful for someone. http://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android/12739968#12739968 – Pramod Moolekandathil Feb 26 '17 at 17:56

0 Answers0