4

I've found a few topics about making Path a Serializable object but I couldn't find anything about making it Parcelable. From what I've read, Parcelable objects are more efficient for packing and unpacking in a Bundle.

I've made a new object called ParcelablePath, which extends 'Path' and implements Parcelable and have overridden the required methods, but I'm not really sure what to do in the overridden methods.

My question is, is it even possible to make Path a Parcelable object? If so how can I do it?

Here is some sample code:

public class ParcelablePath extends Path implements Parcelable {
    protected ParcelablePath(Parcel in) {
    }

    public static final Creator<ParcelablePath> CREATOR = new Creator<ParcelablePath>() {
        @Override
        public ParcelablePath createFromParcel(Parcel in) {
            return new ParcelablePath(in);
        }

        @Override
        public ParcelablePath[] newArray(int size) {
            return new ParcelablePath[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {

    }
}
Community
  • 1
  • 1
Schadenfreude
  • 1,522
  • 1
  • 20
  • 41
  • It may help to read: http://stackoverflow.com/questions/3323074/android-difference-between-parcelable-and-serializable – Morrison Chang Dec 07 '15 at 15:26
  • *but I'm not really sure what to do in the overridden methods* save the state :) so you would be able to redo those opperation in readParcel ... the easiest way would be save(put into ArrayList) object which is also parcelable and saves *the method name and params* ... then the Path would save this array to the parcel ... on reading you would recreate this ArrayList and then call all the method by name from Path with given parameters – Selvin Dec 07 '15 at 15:32
  • look out on pitfalls like: `public void addOval(RectF oval, Direction dir) { addOval(oval.left, oval.top, oval.right, oval.bottom, dir); }` .... if you override both `addOval(RectF oval, Direction dir)` and `addOval(float left, float top, float right, float bottom, Direction dir)` there will be a problem :) (obviously path would then contains 2 ovals ) – Selvin Dec 07 '15 at 15:37
  • @Morrison Chang, as I said, I know the difference between `Serializable` and `Parcelable`, the problem is the implementation of `Parcelable` and if it's at all possible for this class. – Schadenfreude Dec 08 '15 at 10:04

1 Answers1

-1

If you are using Android Studio there's a nice plugin which will solve your issue and automate the process for making a class Parcelable.

You can have a look here: Quickly Create Parcelable Class in Android Studio Android Tutorials Mobile Development

Community
  • 1
  • 1
sliminem
  • 9
  • 3
  • This will not apply here as Path is not a simple container ... also if you take a look at the Path source you will know that it uses native (C/C++) code ... – Selvin Dec 07 '15 at 15:28