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
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)