How to implement undo/redo and preserve the value on orientation change in android EditText?

南楼画角 提交于 2019-12-24 11:23:03

问题


I am using the TextViewUndoRedo class for undo/redo operations and it works but I want the value preserved after orientation/onConfigurationChange.

There are two methods in that class: storePersistentState(Editor editor, String prefix) and restorePersistentState(SharedPreferences sp, String prefix), what do they do?

I guess, these are for onConfigurationChanged and implement in the following way, but they didn't work?

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    vNoteText = vEditNote.getText().toString();

    //----------------------------------------------------------------//

    SharedPreferences sp = getSharedPreferences("unforedopref", 0);
    SharedPreferences.Editor editor;
    editor = sp.edit();

    mTextViewUndoRedo.storePersistentState(editor, "undoredokey");

    //----------------------------------------------------------------//

    setContentView(R.layout.notepadmain);


    vEditNote.setText(vNoteText);

    //----------------------------------------------------------------//    

    mTextViewUndoRedo.restorePersistentState(sp, "undoredokey");
}       

If you provide a undo redo technique which works onOrientationChange with example, it would be helpful.


回答1:


@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
  SharedPreferences sp = getSharedPreferences("unforedopref", 0);

  mTextViewUndoRedo.storePersistentState(sp.edit(), "undoredokey");
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);

  SharedPreferences sp = getSharedPreferences("unforedopref", 0);
  mTextViewUndoRedo.restorePersistentState(sp, "undoredokey");
}

You need to save and restore state in lifecycle methods of your activity



来源:https://stackoverflow.com/questions/27199742/how-to-implement-undo-redo-and-preserve-the-value-on-orientation-change-in-andro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!