0

I'm trying to use Icepick library on our application to save and restore bundle on phone rotation, but the library documentation i cant find good tips to save and classes which implemented by @Parcel annotation, this is simple class which i found o the page

Example class:

@Parcel
public class Example {
    String name;
    int age;

    public Example(){ /*Required empty bean constructor*/ }

    public Example(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public String getName() { return name; }
    public int getAge() { return age; }

    public String setName(String name) { this.name = name; }
    public int setAge(int age ) { this.age = age; }
}

ExampleBundler class:

public class ExampleBundler implements Bundler<Object> {
    @Override
    public void put(String s, Object example, Bundle bundle) {
        bundle.putParcelable(s, Parcels.wrap(example));
    }

    @Override
    public Object get(String s, Bundle bundle) {
        return Parcels.unwrap(bundle.getParcelable(s));
    }
}

Ok, now how can i use that on Activity ?

this is my simple code which i try to save and restore

public class MainActivity extends BaseActivity {

  /* @State(ExampleBundler.class) String message; */

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Example example = new Example();
    example.setName = "HELLO WORLD";
    example.setAge = 99;
  }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    public void onRestoreInstanceState(Bundle inState) {
    }
}

could you fix my code? Thank to advance

Volo
  • 28,673
  • 12
  • 97
  • 125
mahdi pishguy
  • 994
  • 1
  • 14
  • 43
  • You also need to call `Icepick.restoreInstanceState(this, savedInstanceState)` in onCreate and `Icepick.saveInstanceState(this,outstate)` in the onSaveInstanceState – Sahith Reddy Jul 26 '16 at 15:37
  • @SahithReddy and how can i fox this line: `@State(ExampleBundler.class) String message;` – mahdi pishguy Jul 26 '16 at 15:39
  • If it is just a string you do not even need the custom bundler you can just use @State. custom bundler is if you want to save a whole object in conjunction with parcelable – Sahith Reddy Jul 26 '16 at 15:42
  • @SahithReddy this is String, but its wrong and i must fix that, `@state` must be as Parcel or other type. i dont know – mahdi pishguy Jul 26 '16 at 15:53
  • ok sorry I understand the problem. First, I think you should `ExampleBundler implements Bundler` should be `ExampleBundler implements Bundler`. So then in the overrided methods you can use the Example object instead of the general Object. Then, add this `@State(ExampleBundler.class) Example example;` instead of the comment you have. Finally, add the first comment i posted about Icepick restore and save. If you want more detail I can post an actual answer instead of comments. – Sahith Reddy Jul 26 '16 at 16:01
  • @SahithReddy ok, i understand me sir, thanks, can you post answer and let me to accept that? if i have problem i can post comment under your answer – mahdi pishguy Jul 26 '16 at 16:06

1 Answers1

3

To fix your code, the bundler has to implement the example class not the general object class so change

    public class ExampleBundler implements Bundler<Object> {
    @Override
    public void put(String s, Object example, Bundle bundle) {
        bundle.putParcelable(s, Parcels.wrap(example));
    }

    @Override
    public Object get(String s, Bundle bundle) {
        return Parcels.unwrap(bundle.getParcelable(s));
    }
}

to this

public class ExampleBundler implements Bundler<Example> {
    @Override
    public void put(String s, Example example, Bundle bundle) {
        bundle.putParcelable(s, Parcels.wrap(example));
    }

    @Override
    public Example get(String s, Bundle bundle) {
        return Parcels.unwrap(bundle.getParcelable(s));
    }
}

Then, you have to add @State(ExampleBundler.class) Example example; to the top of your activity. In your onCreate add Icepick.restoreInstanceState(this, savedInstanceState) and in your onSavedInstanceState add Icepick.saveInstanceState(this,outstate)

Icepick documentation and parceler

Sahith Reddy
  • 263
  • 3
  • 10