从Edittext保存最新文本并在onDestroy之后恢复它

时间:2016-02-02 08:48:03

标签: android android-edittext state

对于我的引用创建者应用程序,如果用户在应用程序中写入文本并意外关闭应用程序并再次启动它,则所有文本都将消失。我想要阻止这种情况,并且我尝试了一种没有过头的共同解决方案:

这是我到目前为止所做的。我删除了不相关的代码。

public static EditText mEditText;
private String savedText;
private static final String SAVED_TEXT_KEY = "";

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString(SAVED_TEXT_KEY, mEditText.getText().toString());
    savedText = SAVED_TEXT_KEY;

}

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

    mEditText.setText(SAVED_TEXT_KEY);
    String myString = savedInstanceState.getString(SAVED_TEXT_KEY);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    mEditText = (EditText) findViewById(R.id.edittext);
    mEditText.setTypeface(Style.getTypeface(this, Style.SERIF));
    savedText = mEditText.getText().toString();
    mEditText.setText(SAVED_TEXT_KEY);



    if(savedInstanceState !=null){

        savedText = savedInstanceState.getString(SAVED_TEXT_KEY);

    }
}

6 个答案:

答案 0 :(得分:4)

似乎我对这个问题采取了错误的方法。正如所建议的,我使用SharedPreferences来保存当前文本。

   public static final String LAST_TEXT = "";

    final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
    mEditText.setText(pref.getString(LAST_TEXT, ""));
    mEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

            pref.edit().putString(LAST_TEXT, s.toString()).commit();


        }
    });

答案 1 :(得分:1)

如果我没有错,那么你就不要再在textview上设置文字了。

@Override
public void onSaveInstanceState(Bundle outState) {
  outState.putString(SAVED_TEXT_KEY, mEditText.getText().toString());
  super.onSaveInstanceState(outState);
}

public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  mEditText.setText(savedInstanceState.getString(SAVED_TEXT_KEY));
}

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  mEditText = (EditText) findViewById(R.id.edittext);
  mEditText.setTypeface(Style.getTypeface(this, Style.SERIF));
  if (savedInstanceState != null) {
    mEditText.setText(savedInstanceState.getString(SAVED_TEXT_KEY));
  }
}

这应该可以解决问题。

答案 2 :(得分:1)

首先,您应为SAVED_KEY_TEXT提供正确的密钥,而不是空字符串。

private static final String SAVED_TEXT_KEY = "some_key";

然后,您应该将onCreate更新为:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    mEditText = (EditText) findViewById(R.id.edittext);
    mEditText.setTypeface(Style.getTypeface(this, Style.SERIF));

    if(savedInstanceState !=null){
        mEditText.setText(savedInstanceState.getString(SAVED_TEXT_KEY));
    }
}

你的onRestoreInstanceState就是这样:

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

    mEditText.setText(savedInstanceState.getString(SAVED_TEXT_KEY));
}

最后你的onSaveInstanceState到此:

public void onSaveInstanceState(Bundle outState) {
    outState.putString(SAVED_TEXT_KEY, mEditText.getText().toString());
    super.onSaveInstanceState(outState);
}

答案 3 :(得分:0)

试试这个:

通过覆盖此保存您的状态:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString(KEY, mEditText.getText().toString());
}

并恢复它:

public void onCreate(Bundle savedInstanceState) {
    if(savedInstanceState != null)
    {
        mEditText.setText(savedInstanceState.getString(KEY));
    }
}

答案 4 :(得分:0)

public EditText mEditText;
private static final String SAVED_TEXT_KEY = "SAVED_TEXT_KEY";
private SharedPreferences prefs;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mEditText = (EditText) findViewById(R.id.editText);
    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    mEditText.setText(prefs.getString(SAVED_TEXT_KEY,""));
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString(SAVED_TEXT_KEY, mEditText.getText().toString());
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    mEditText.setText(savedInstanceState.getString(SAVED_TEXT_KEY));
}

@Override
public void onDestroy() {
    super.onDestroy();
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(SAVED_TEXT_KEY,mEditText.getText().toString());
    editor.commit();
}

答案 5 :(得分:0)

使用IcePick Library:https://github.com/frankiesardo/icepick

@State String username; //这将自动保存并恢复