Android 使用 Icepick 库 save/restore 打包
Android use Icepick library to save/restore bundle
我正在尝试在我们的应用程序上使用 Icepick
库来保存和恢复包 phone 轮换,但是我找不到保存和 class 的好的库文档es 由 @Parcel
注释实现,这很简单 class 我在页面
上找到的
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));
}
}
好的,现在我如何在 Activity
上使用它?
这是我尝试保存和恢复的简单代码
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) {
}
}
你能修复我的代码吗?感谢提前
要修复您的代码,捆绑器必须实现示例 class 而不是一般对象 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));
}
}
至此
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));
}
}
然后,您必须将 @State(ExampleBundler.class) Example example;
添加到 activity 的顶部。在您的 onCreate 添加 Icepick.restoreInstanceState(this, savedInstanceState)
并在您的 onSavedInstanceState 添加 Icepick.saveInstanceState(this,outstate)
我正在尝试在我们的应用程序上使用 Icepick
库来保存和恢复包 phone 轮换,但是我找不到保存和 class 的好的库文档es 由 @Parcel
注释实现,这很简单 class 我在页面
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));
}
}
好的,现在我如何在 Activity
上使用它?
这是我尝试保存和恢复的简单代码
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) {
}
}
你能修复我的代码吗?感谢提前
要修复您的代码,捆绑器必须实现示例 class 而不是一般对象 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));
}
}
至此
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));
}
}
然后,您必须将 @State(ExampleBundler.class) Example example;
添加到 activity 的顶部。在您的 onCreate 添加 Icepick.restoreInstanceState(this, savedInstanceState)
并在您的 onSavedInstanceState 添加 Icepick.saveInstanceState(this,outstate)