我想写一些帮助程序来保存和恢复 Bundle 中的活动状态
重写方法onCreate(Bundle savedState)
仍然onSaveInstanceState(Bundle outState)
是必要的,但是简单的表单保存/恢复有点无聊
像这样的东西:
class StateHelper {
static void restore(Bundle bundle, String[] properties, Object[] connections){
for(int i = 0; i < properties.length; i++){
if(bundle.containsKey(properties[i])){
restoreState(properties[i], connections[i]);
}
}
}
static void save(Bundle bundle, String[] properties, Object[] connections){
for(int i = 0; i < properties.length; i++){
saveState(properties[i], connections[i]);
}
}
restoreState(String s, Object o){
if(o instanceof EditText){
// restore state with getString
} else if(o instanceof Checkbox){
// save state with getBoolean
}
// etc. etc. handle all UI types
}
saveState(String s, Object o){
// similar to restoreState(String, Object)
// only saving instead of restoring
}
}
并像这样使用:
String[] props = {LOGIN,PASSWORD,REALNAME};
Object[] cons = {textedit_login, textedit_password, textedit_realname};
StateHelper.restore(savedState, props, cons);
// or
StateHelper.save(outBundle, props, cons);
在我花一整天时间创建它之前,我的问题是,是否有任何类似的帮助类或本地方式来执行这个简单的保存/恢复操作?