Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

java - onSaveInstanceState() not saving data from the intent

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...