0

完全卡住了保存和恢复活动的实例状态。这是我所拥有的:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register1);

    context = this;

    input_first_name = (EditText) findViewById(R.id.first_name);
    input_last_name = (EditText) findViewById(R.id.last_name);
    input_email = (EditText) findViewById(R.id.register_email);
    input_password = (EditText) findViewById(R.id.register_password);
    input_cell = (EditText) findViewById(R.id.register_cell);

    if (savedInstanceState != null) {
        System.err.println(savedInstanceState.getString("first_name"));
        input_first_name.setText(savedInstanceState.getString("first_name"));
        input_last_name.setText(savedInstanceState.getString("last_name"));
        input_email.setText(savedInstanceState.getString("email"));
        input_password.setText(savedInstanceState.getString("password"));
        input_cell.setText(savedInstanceState.getString("cell"));
    }
    ...
}

protected void onSaveInstanceState(Bundle b) {
    super.onSaveInstanceState(b);
    System.err.println("save called");

    b.putString("first_name", first_name);
    b.putString("last_name", last_name );
    b.putString("email", email);
    b.putString("password", password);
    b.putString("cell", cell);
}

protected void onRestoreInstanceState(Bundle b) {
    System.err.println(b.getString("first_name"));
    input_first_name.setText(b.getString("first_name"));
    input_last_name.setText(b.getString("last_name"));
    input_email.setText(b.getString("email"));
    input_password.setText(b.getString("password"));
    input_cell.setText(b.getString("cell"));
}

我得到了“保存调用”的输出,所以我认为我的包被正确保存了。但是,当我回到活动中时,我从未看到任何输出。有人看到我做错了吗?谢谢!

4

2 回答 2

0

尝试此解决方案以确保您onRestoreInstanceState正确使用。

于 2013-08-14T20:13:28.020 回答
0

我真的没有看到任何错误添加@Override 以确保它在您的类中调用该函数,而不是在超类中调用该函数

如下:

@Override
protected void onSaveInstanceState(Bundle b) {
super.onSaveInstanceState(b);
System.err.println("save called");

b.putString("first_name", first_name);
b.putString("last_name", last_name );
b.putString("email", email);
b.putString("password", password);
b.putString("cell", cell);
}
于 2013-08-14T20:03:03.683 回答