我想寫某種幫手來保存和/恢復活動狀態的線束活動狀態保存和恢復助手
重寫方法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);
然後我會花一整天的時間創建這個,我的問題是,有沒有類似的幫助類或本地方式如何做到這一點簡單的保存/恢復操作?
啊,錯過了那一點。謝謝 –