Save latest text from Edittext and restore it afte

2020-07-27 05:19发布

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);

    }
}

6条回答
冷血范
2楼-- · 2020-07-27 05:45

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));
    }
}
查看更多
我命由我不由天
3楼-- · 2020-07-27 05:48

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.

查看更多
Explosion°爆炸
4楼-- · 2020-07-27 06:04

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();


        }
    });
查看更多
兄弟一词,经得起流年.
5楼-- · 2020-07-27 06:04

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);
}
查看更多
聊天终结者
6楼-- · 2020-07-27 06:05

Use IcePick Library :https://github.com/frankiesardo/icepick

@State String username; // This will be automatically saved and restored

查看更多
霸刀☆藐视天下
7楼-- · 2020-07-27 06:08
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();
}
查看更多
登录 后发表回答