[英]Save latest text from Edittext and restore it after onDestroy
For my quote creator app, if the user wrote a text in the app and by accident closed the app and started it again, all the text will be gone. 对于我的报价创建者应用程序,如果用户在应用程序中编写了文本,然后无意中关闭了该应用程序并再次启动它,则所有文本都将消失。 I want ofcourse to prevent that, and I have tried a common solution without succses:
我当然想防止这种情况,并且我尝试了一种不带任何成功的通用解决方案:
This is what I have done so far. 到目前为止,这是我所做的。 I have removed unrelated code.
我删除了不相关的代码。
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);
}
}
It seems to be that I had a wrong approach for this problem. 看来我对这个问题有错误的处理方法。 As suggested I used SharedPreferences to save the current text.
根据建议,我使用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();
}
});
If I am not wrong then you don't set the text on the textview again. 如果我没有记错的话,那么您无需在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));
}
}
This should do the trick. 这应该可以解决问题。
First, you should give a proper key, not an empty string, for SAVED_KEY_TEXT. 首先,您应该为SAVED_KEY_TEXT提供正确的密钥,而不是空字符串。
private static final String SAVED_TEXT_KEY = "some_key";
Then, you should update your onCreate to this: 然后,您应该将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));
}
}
And your onRestoreInstanceState to this: 和您的onRestoreInstanceState到此:
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mEditText.setText(savedInstanceState.getString(SAVED_TEXT_KEY));
}
And at last your onSaveInstanceState to this: 最后,您的onSaveInstanceState与此:
public void onSaveInstanceState(Bundle outState) {
outState.putString(SAVED_TEXT_KEY, mEditText.getText().toString());
super.onSaveInstanceState(outState);
}
Try this: 尝试这个:
Save your state by override this: 通过覆盖以下内容来保存状态:
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(KEY, mEditText.getText().toString());
}
and restore it: 并恢复它:
public void onCreate(Bundle savedInstanceState) {
if(savedInstanceState != null)
{
mEditText.setText(savedInstanceState.getString(KEY));
}
}
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();
}
Use IcePick Library : https://github.com/frankiesardo/icepick 使用IcePick库: https : //github.com/frankiesardo/icepick
@State String username; @State字符串用户名; // This will be automatically saved and restored
//这将自动保存和还原
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.