onSaveInstanceState()不保存意图中的数据

时间:2015-05-23 18:23:23

标签: java android android-intent

我有一张表格。在这里,用户填写诸如事件名称,描述,位置等的详细信息。为了选择位置,我设置了一个按钮。当我点击按钮时,我被带到另一个页面/意图。我从那里选择位置,并将位置数据正确地传回我的原始意图。但问题是,用户填写的其他字段中的数据(如事件,描述等)将丢失。我之前发过了this个问题并尝试了这些问题。我也提到了this并尝试使用onSaveInstanceState这样:

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

我在onCreate()之前有这个功能;这是我的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
    }

它不起作用。有人可以指出为什么吗?

1 个答案:

答案 0 :(得分:0)

不确定是否让它工作。但我得到了这个。

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