Reputation:
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);
}
}
Upvotes: 2
Views: 9895
Reputation:
It seems to be that I had a wrong approach for this problem. As suggested I used SharedPreferences to save the current text.
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();
}
});
Upvotes: 5
Reputation: 13
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();
}
Upvotes: 0
Reputation: 1020
First, you should give a proper key, not an empty string, for SAVED_KEY_TEXT.
private static final String SAVED_TEXT_KEY = "some_key";
Then, you should update your onCreate to this:
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:
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mEditText.setText(savedInstanceState.getString(SAVED_TEXT_KEY));
}
And at last your onSaveInstanceState to this:
public void onSaveInstanceState(Bundle outState) {
outState.putString(SAVED_TEXT_KEY, mEditText.getText().toString());
super.onSaveInstanceState(outState);
}
Upvotes: 1
Reputation: 97
Use IcePick Library :https://github.com/frankiesardo/icepick
@State String username; // This will be automatically saved and restored
Upvotes: 0
Reputation: 5297
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));
}
}
Upvotes: 0
Reputation: 8073
If I am not wrong then you don't set the text on the textview again.
@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.
Upvotes: 2