Reputation: 1950
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) {
}
}
Upvotes: 4
Views: 926
Reputation: 38243
If you need to build a path consisting only of straight lines and marshal it to another process, you can use Gesture
.
Gesture
implements Parcelable
so can be sent accross process boundaries in a Parcel
and provides a toPath
method to construct a Path
at the other end.
// build path
val points = ArrayList<GesturePoint>()
points += GesturePoint(x = 0f, y = 0f, timestamp = 0L)
// make parcelable
val stroke = GestureStroke(points)
val gesture = Gesture().apply { addStroke(stroke) }
// send away
You could make this an implementation detail of your ParcelablePath
which would no longer extend Path
and only expose toPath
and a limited subset of the Path
API.
Upvotes: 1
Reputation: 7
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
Upvotes: -2