onSaveInstanceState() not saving data from the int

2019-09-20 08:44发布

问题:

I have a form. Here, the user fills in details like event name, description, location etc. To select the location, I have set a button. When I click on the button, I am taken to another page/intent. I select the location from there, and the location data is passed back to my original intent correctly. But the thing is, the data in the other fields like event, description etc. that users filled in are lost. I posted this question before and tried those out. I referred to this too and tried using onSaveInstanceState like this:

private static final String ENAME = "ENAME";
private static final String DESC = "DESC";
private static final String SDATE = "SDATE";
private static final String EDATE = "EDATE";
private static final String STIME = "STIME";
private static final String ETIME = "ETIME";

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    final String name = etename.getText().toString();
    savedInstanceState.putString(ENAME, name);
    final String des = etdes.getText().toString();
    savedInstanceState.putString(DESC, des);
    final String sdt = StartDate.getText().toString();
    savedInstanceState.putString(SDATE, sdt);
    final String edt = EndDate.getText().toString();
    savedInstanceState.putString(EDATE, edt);
    final String stm = StartTime.getText().toString();
    savedInstanceState.putString(STIME, stm);
    final String etm = EndTime.getText().toString();
    savedInstanceState.putString(ETIME, etm);

    // Put (save) other values into the outState bundle...

    super.onSaveInstanceState(savedInstanceState);
}

I have that function before the onCreate(); And here is my onCreate();

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.eventcreaten);

    etename = (EditText) findViewById(R.id.etename);
    etdes = (EditText) findViewById(R.id.etdes);
    StartDate = (EditText) findViewById(R.id.etstartdate1);
    EndDate = (EditText) findViewById(R.id.etenddate1);
    btnCreateEvent = (Button) findViewById(R.id.btncreate);
    tvd2 = (TextView) findViewById (R.id.tvd2);
    tved2 = (TextView) findViewById (R.id.tved2);
    tvg = (TextView) findViewById (R.id.tvg);
    StartTime = (EditText) findViewById (R.id.etstartt1);
    EndTime = (EditText) findViewById (R.id.etendt1);


    if (savedInstanceState != null) {
        final String name = savedInstanceState.getString(ENAME, "");
        etename.setText(name);
        final String des = savedInstanceState.getString(DESC, "");
        etdes.setText(des);
        final String sdt = savedInstanceState.getString(SDATE, "");
        StartDate.setText(sdt);
        final String edt = savedInstanceState.getString(EDATE, "");
        EndDate.setText(edt);
        final String stm = savedInstanceState.getString(STIME, "");
        StartTime.setText(stm);
        final String etm = savedInstanceState.getString(ETIME, "");
        EndTime.setText(etm);
    } else {
        // No saved values to restore
    }

And it doesn't work. Can someone please point out why?

回答1:

Not sure if you got it to work or not. But I got it to work with this.

public class TestingForSO extends ActionBarActivity {
    private static final String ENAME = "ENAME";
    EditText etename;
    String name = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_testing_for_so);
        if (savedInstanceState != null) {
            name = savedInstanceState.getString(ENAME, "");
            Log.e("NAME", "saved");
        } else{
            Log.e("NAME", "not_saved");
        }
        etename = (EditText) findViewById(R.id.etename);
        etename.setText(name);
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        Log.e("NAME", "Saving");
        savedInstanceState.putString(ENAME, etename.getText().toString());
    }
}