Skip to content Skip to sidebar Skip to footer

How Do I Send An Object With Arbitrary Types In It To Another Activity?

I'm pretty new to Android programming and I've ran into this issue. I have an object I am trying to send into a new activity, which is an instance of this class: public final class

Solution 1:

EventBus makes your life so much easier, see the example and links in my answer to this question: Saving information from one fragment and dialog if the user navigates to another fragment in essence, no need to do any serialization or anything, just put the object on the eventbus and grab it again anywhere in your code (in another Activity, Fragment, Service etc..)

Solution 2:

If you are able to modify BusinessEntity, then have it implement Serializable. You may then put the business as an extra:

//To put the object as an extra
...
BusinessEntitybusiness= businesses(get(position));
Intentintent=newIntent(mActivity, DetailsActivity.class);
intent.putExtra("business", business); // Where business is now a `Serializable`
...

//To retrieve the object (in your second class). //TODO -- Include check to see if intent has the extra firstBusinessEntityretrievedBusiness= (BusinessEntity) intent.getSerializableExtra("business")

Solution 3:

Solution 4:

You can do this via Serializable interface. Just let your BusinessEntity implement Serializable, like this:

publicfinalclassBusinessEntityextendscom.google.api.client.json.GenericJson implementsSerializable {
     //your code here
     ...
     ...
}

Then create your intent and put an extra to it:

Intent i = newIntent(mActivity, DetailsActivity.class);
i.putExtra("BusinessEntity", yourBuisnessEntityObject);
startActivity(i);

And finally in your DetailsActivity:

BusinessEntitybusiness= (BusinessEntity) getIntent().getSerializableExtra("BusinessEntity");

Voila! You have your BusinessEntity object in DetailsActivity.

EDIT:

Back to your code, I think the problem is that you put a JsonObject extra not a JsonArray. You should do the same things that you posted firstly, but with one correction:

JsonObject object = parser.parse(json).getAsJsonObject(); and then parse it as JsonObject by keys.

Post a Comment for "How Do I Send An Object With Arbitrary Types In It To Another Activity?"