Reputation: 2092
My app queries a service using an external library in the form of a JAR file. When I query this service, I get back a List of objects (object is used generically, not Object), which do not implement Parcelable in their current form. I need to send one of these objects between activities, so I am assuming that I need to somehow make them implement Parcelable. However, I'm not sure how to do so if they are not my objects, and all the tutorials I've found online seem to only deal with objects that the author created for his own project.
Any help would be greatly appreciated. Let me know if I need to clarify something.
Upvotes: 4
Views: 913
Reputation: 64710
Assuming you can instantiate new copies of the object yourself, you can always use composition and implement your own Parcelable data transfer object to do the transfer:
Create a new Parcelable class, let's call it PObject.
Add a constructor for PObject that takes in a copy of the object that does not implement Parcelable.
Create a method on PObject that returns a fully instantiated copy of the object that does not implement Parcelable.
You should now be able to use the PObject implementation to transfer the objects.
The documentation of the Parcelable interface shows a basic example to transfer an integer.
Of course, this assumes you need Parcelable: if the Activities are in the same process you can probably just pass them using a global static. Its not the prettiest, but its often good enough.
Upvotes: 3
Reputation: 25761
Another alternative would be to use your Application to store these objects. Simple subclass Application, make it a singleton and declare it in your Manifest.xml
public class MyApplication extends Application {
private static final MyApplication instance;
public static MyApplication getInstace() {
return instance;
}
public void onCreate() {
instance = this;
}
private MyObject myObj;
public MyObject getMyObject() {
return this.myObj;
}
public void setMyObject(MyObject myObj) {
this.myObj = myObj;
}
}
Then later on you can store it:
MyApplication.getInstance().setMyObject(anObject);
And recover it:
MyObject anObject = MyApplication.getInstance().getMyObject();
Remember that:
Upvotes: 2