12

i am trying to pass the gson serialised object to intent by using the below code

intent.putExtra("com.example", vo); // vo is the gson serialised object.

but it is throwing the run time exception, Please help.

   java.lang.RuntimeException: Parcelable encountered IOException writing serializable    object (name = com.example.d.a)
            at android.os.Parcel.writeSerializable(Parcel.java:1285)
            at android.os.Parcel.writeValue(Parcel.java:1233)
            at android.os.Parcel.writeMapInternal(Parcel.java:591)
            at android.os.Bundle.writeToParcel(Bundle.java:1646)
            at android.os.Parcel.writeBundle(Parcel.java:605)
            at android.content.Intent.writeToParcel(Intent.java:6831)
            at     android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1927)
            at android.app.Instrumentation.execStartActivity(Instrumentation.java:1419)
            at android.app.Activity.startActivityForResult(Activity.java:3390)
            at android.app.Activity.startActivityForResult(Activity.java:3351)
            at android.app.Activity.startActivity(Activity.java:3587)
            at android.app.Activity.startActivity(Activity.java:3555)
Reed
  • 14,703
  • 8
  • 66
  • 110
ida
  • 545
  • 2
  • 5
  • 20

3 Answers3

43

No you are using it in the wrong way.

Put the object in the intent as:

Gson gson = new Gson();
Intent intent = new Intent(Source.this, Target.class);
intent.putExtra("obj", gson.toJson(yourObject));

and get the object in another activity as:

Gson gson = new Gson();
String strObj = getIntent().getStringExtra("obj");
SourceObject obj = gson.fromJson(strObj, SourceObject.class);
Alexander Zhak
  • 9,140
  • 4
  • 46
  • 72
NullPointerException
  • 3,978
  • 4
  • 34
  • 52
  • I am doing this the same way in my app. I've got a list, where I have a preview of the items. When the user clicks in some of them, I display a new detail Activity. To avoid calling the API once per each item the user clicks, I've used this technique with Gson. My question is... Is there a better way to do this? Which one is better? Calling the API again or serialising/deserialising with Gson? Thanks. – Kashmir Apr 18 '17 at 13:48
3

I added implements Serializable to my model AND other models (sub-models) used by my model. Then I can pass the GSON object via Intent:

public class MyModel implements Serializable {
    SubModel subModel;
}

public class SubModel implements Serializable {
    ...
}

In fragment:

Intent startIntent = new Intent(getContext(), NextActivity.class);
startIntent.putExtra("mymodel", myModelObject);
startActivity(startIntent);

In next activity:

Intent intent = getIntent();
MyModel mymodel = (MyModel) intent.getSerializableExtra("mymodel");
// test if we get the model correctly:
setTitle(mymodel.getName());
John Pang
  • 2,403
  • 25
  • 25
1

When your object or Model is extend RealmObject you need to use that:

Step1:

 Gson gson = new GsonBuilder() .setExclusionStrategies(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes f) {
    return f.getDeclaringClass().equals(RealmObject.class);
}

@Override
public boolean shouldSkipClass(Class<?> clazz) {
    return false;
}
})
.create();

And

 intent.putExtra("userfrom",gson.toJson(obj));

Step2:

Gson gson = new GsonBuilder().create();



 user =gson.fromJson(getIntent().getStringExtra("userfrom"),User.class);

I used this for pass data with Intent but work for retrofit

Fidan Bacaj
  • 408
  • 5
  • 10