2

I'm looking for a way of passing an object that I didn't create and cannot modify to implement parcelable in android. I was given a jar file that placed into the project by building a path to it. Now i need to pass the object created from activity to activity so that I may use the contents of the jar file. Right now it is set up so I define it as static, which probably isn't the best way. The only other option I can think of is using putSerializable but I've heard that puts strain on the system. So, what are my other options?

thad
  • 167
  • 1
  • 4
  • 16
  • If the class does not implement `Parcelable` then I'm afraid serializing it is your only option. – m0skit0 Jul 18 '13 at 18:17
  • Extend the class and implement parceable there. You could implement inner classes to deal with anything more complex than primitive fields? – Simon Jul 18 '13 at 18:17
  • Can you briefly explain what kind of object you are talking about? Because solution for your problem depends on what you are actually passing (some stateless object or actual data?) – Dmitry Zaytsev Jul 18 '13 at 18:17
  • @Simon extending the class won't help. What about private fields? – m0skit0 Jul 18 '13 at 18:18
  • http://stackoverflow.com/questions/1196192/how-do-i-read-a-private-field-in-java – Simon Jul 18 '13 at 19:04

1 Answers1

0

The main problem you have here is if that class has non-accessable private fields (through getters), then you cannot get this data to parcel it. If all private fields are accessable, then you might have several possibilities:

  • Extending it with a Parcelable subclass (as suggested by Simon in the comments).
  • Wrapping it in another Parcelable object.
  • Converting it to an already Parcelable object (e.g. any implementation of Map)

Note that if the object itself is not very big then the performance drop between parcelling and serializing shouldn't be noticeable. So I would go for Serializing and if the performance is not satisfactory then consider other options.

m0skit0
  • 25,268
  • 11
  • 79
  • 127
  • the item does have getters and setters. I think I'll go with serializing it for now and if I have time(It's a competition) then I'll go back and use one of your first two suggestions. Out of your first two suggestions which one would you recommend? – thad Jul 18 '13 at 18:53
  • Getters annd setters for **all** the fields I meant. Both are good but I like more Simon's suggestion of extending, it's more OOP. – m0skit0 Jul 18 '13 at 19:30