I am using parceler library . I have made an complex object with this . As it says it makes the object parcelable , so I want to use it for saving fragment state .
Here is my model
@Parcel
public class Example {
String name;
int age;
public Example() {}
public Example(int age, String name) {
this.age = age;
this.name = name;
}
public String getName() { return name; }
public int getAge() { return age; }
}
And in my fragment I have this
ArrayList<Example> exampletLists;
But when I try to put it in onSaveInstanceState
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelableArrayList("EXAMPLE_LIST",exampletLists); //this is what I want to do , but I can't
}
And I want to get the value in onCreate Like
if (savedInstanceState != null) {
exampletLists = savedInstanceState.getParcelableArrayList(EXAMPLE_LIST);
}
How can I achieve this with this libray ?
Parceler can wrap ArrayLists, so what you can do is use the Parcels.wrap()
and Parcels.unwrap()
methods when writing and reading your `savedInstanceState:
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable("EXAMPLE_LIST", Parcels.wrap(exampletLists));
}
public void onCreate(Bundle savedInstanceState) {
//...
if (savedInstanceState != null) {
exampletLists = Parcels.unwrap(savedInstanceState.getParcelable(EXAMPLE_LIST));
}
}