1
  1. I'm using an external library for 3rd party map retrieving in Android.
  2. There's a class that I want to pass through Intent, so I want to make it implement Parcelable.
  3. But the class is from the external library.

What's the best practice to do in this case?

What I did was:

  1. Create a custom class that extends this class(let me call it A) and implement Parcelable
  2. But when I get all these A class instances from the library's network request, I found out that I can't cast it to my custom class object since it's casting from parent class to child class.
  3. So I added a static method that can copy member variables from A class to newly instantiated custom object's member variables, which literally converts external library class A into my custom class.
  4. It does work, but it looks like a code smell. How can I improve this? Or is it not a code smell?
viz
  • 1,247
  • 16
  • 27
  • (But when I get all these A class instances from the library's network request, I found out that I can't cast it to my custom class object since it's casting from parent class to child class), You can down casting in java, Did you tried. – Fady Saad Jun 12 '17 at 05:52
  • 2
    @user7790438 for sure, but when you, let's say `A a = new A(); B b = (B)a;`(when b is a child class of A), down casting does not work. It only works when `A a = new B(); B b = (B)a;`. And I don't have control over the instantiation phase of the `A` class. The library gave it to me in the form of `ArrayList` after network request as a callback. – viz Jun 12 '17 at 05:55
  • 1
    Then you don't choice than the one you did. – Fady Saad Jun 12 '17 at 05:57
  • yeah maybe it's best I can do – viz Jun 12 '17 at 06:10

1 Answers1

-1

Ideally any pojo you use in android be parcelable. If not, the way you have done is an alternative and is not a code smell. Chill

Mukund Desai
  • 100
  • 2