-2

I've a class Testendride:

public Testendride guranteBoneses;

set the values on response

JSONObject jsonObject = new JSONObject(s);
Gson gc = new Gson();
guranteBoneses = gc.fromJson(s, Testendride.class);

I'd like to send this Testendride object to the next Activity

Intent intent = new Intent(ActivityGuaranteeBonesesOffers.this, ActivityDetailGaurantee.class);
intent.putExtra("gurantee","gurantee");
intent.putExtras("key", guranteBoneses);
startActivity(intent);
Benjamin
  • 7,055
  • 6
  • 40
  • 60
Anaya Adana
  • 135
  • 4

3 Answers3

1

You should make your Object implement Parcelable.
A short example how to do it:

public class MyObject implements Parcelable {

    private final String item1;
    private final String item2;

    public MyObject(String item1, String item2) {
        this.item1 = item1;
        this.item2 = item2;
    }

    public String getItem1() {
        return item1;
    }

    public String getItem2() {
        return item2;
    }

    private MyObject(Parcel in) {
        item1 = in.readString();
        item2 = in.readString();
    }

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

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

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(item1);
        dest.writeString(item2);
    }
}  

Then you can use your object like this:

MyObject object = new MyObject("first", "second");
intent.putExtra("myObject", object);  

To get extra use: intent.getParcelableExtra("myObject");

Dumbo
  • 1,630
  • 18
  • 33
0

You need to convert you guaranteeBonueses object to parcelable and pass it to your desired activity. You can read about parcelable in this codepath article You can refer following articles too https://code.tutsplus.com/tutorials/how-to-pass-data-between-activities-with-android-parcelable--cms-29559

https://www.101apps.co.za/articles/passing-data-between-activities.html

If you want to pass the network response to your desired activity , prefer to create a new model and pass it , as it is a good practice to seperate your network layer.

Akshay Mukadam
  • 2,388
  • 1
  • 27
  • 40
0

I simply do like this

Gson gc = new Gson();

            Intent intent = new Intent(ActivityGuaranteeBonesesOffers.this, ActivityDetailGaurantee.class);
            intent.putExtra("gurantee","gurantee");

            intent.putExtra("key", gc.toJson(guranteBoneses));

            startActivity(intent);

And received on second Activity As string and then convert it into object class

 Bundle b = new Bundle();
    b = getIntent().getExtras();
    Gson gc = new Gson();
    guranteBoneses = gc.fromJson(b.getString("key"), Testendride.class);
Anaya Adana
  • 135
  • 4