How to implement Parcelable on an external object?

My application requests a service using an external library as a JAR file. When I request this service, I return a list of objects (the object is used in the general case, not the object) that do not implement Parcelable in their current form. I need to send one of these objects between actions, so I guess I need to somehow get them to implement Parcelable. However, I’m not sure how to do this if they are not my objects, and all the training materials that I found on the Internet seem to deal only with objects created by the author for his own project.

Any help would be greatly appreciated. Let me know if I need to clarify anything.

+4
source share
2 answers

Another alternative is to use your application to store these objects. A simple subclass application, make it single 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 you can save it:

 MyApplication.getInstance().setMyObject(anObject); 

And restore it:

 MyObject anObject = MyApplication.getInstance().getMyObject(); 

Remember, that:

  • Declare the MyApplication class as Application in Manifest.xml
  • If your process is killed or quits, your data will no longer be available.
+2
source

Assuming that you can create new copies of the object yourself, you can always use composition and implement your own packet data transfer object for transferring:

  • Create a new Parcelable class, call it a PObject.
  • Add a constructor for a PObject that accepts a copy of an object that does not implement Parcelable.
  • Create a method in PObject that returns the full instance of an instance of an object that does not implement Parcelable.

    Now you can use the PObject implementation to transfer objects.

The Parcelable interface documentation shows a basic example for integer transfer.

Of course, this assumes you need Parcelable: if the actions are in the same process, you can probably just pass them using the global static. It is not the most beautiful, but its often quite good.

+3
source

All Articles