1

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 ?

Mithun Sarker Shuvro
  • 3,902
  • 6
  • 32
  • 64
  • @shurvo Can you explain your problem briefly? – Ajeet Aug 11 '16 at 05:16
  • I have a recycler view , which is showing some item via web service . Each time I rotate the screen , the web service is called again . In order to avoid that , I have to save state . And the variable I want to set is the List of Example . So I have to make Example class Parcelable , so I can put it into outState.putParcelableArrayList , I have made the class Example Parcelable using this library , I can't put it inside outState.putParcelableArrayList – Mithun Sarker Shuvro Aug 11 '16 at 06:41
  • What error are you getting in putParcelable? – Ajeet Aug 11 '16 at 06:45
  • It is saying exampletLists must be extent Parcelable – Mithun Sarker Shuvro Aug 11 '16 at 07:03

1 Answers1

1

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));
    }
}
John Ericksen
  • 10,995
  • 4
  • 45
  • 75