I made a custom view that can be drawn and the drawing is saved into an arraylist. I want to save those arraylists in onSaveInstanceState and retrive them in onRestoreInstanceState. I have tried using Sharedpreferences with Json and its not working and even parcelable. I want to save the path and the paint.
The pen class:
private static class Pen implements Parcelable {
Path path;
Paint paint;
int color;
float width;
Pen(int colort, float widtht ) {
color = colort;
width = widtht;
path = new Path();
paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(width);
paint.setColor(color);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
}
// De-parcel Pen object
public Pen(Parcel in) {
color = in.readInt();
width = in.readFloat();
path = new Path();
paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(width);
paint.setColor(color);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
}
// Function that writes Dice values to parcel
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(color);
dest.writeFloat(width);
}
// Creator of Parcelable
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Pen createFromParcel(Parcel in) {
return new Pen(in);
}
public Pen[] newArray(int size) {
return new Pen[size];
}
};
// Function to implement Parcelable interface
@Override
public int describeContents() {
return 0;
}
}
OnsaveInstanceState:
@Override
public Parcelable onSaveInstanceState()
{
Bundle bundle = new Bundle();
bundle.putParcelable("superState", super.onSaveInstanceState());
bundle.putParcelableArrayList("pen", mPenList);
return bundle;
}
onRestoreInstanceState:
@Override
public void onRestoreInstanceState(Parcelable state)
{
if (state instanceof Bundle) // implicit null check
{
Bundle bundle = (Bundle) state;
this.mPenList = bundle.getParcelableArrayList("pen");
state = bundle.getParcelable("superState");
}
super.onRestoreInstanceState(state);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// Save the state so that next time we know if we need to refresh data.
outState.putParcelableArrayList("yourkey",yourlist);
);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
list= savedInstanceState.getParcelableArrayList("yourkey");
}
maybe this can help you :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.