为什么保存在onSaveInstanceState()中的数据都返回null

时间:2017-01-29 12:26:28

标签: java android

我用密钥"消息"保存这个字符串。在onSaveInstanceState()方法中简单地在onCreate()方法中显示toast它不起作用。我做错了什么?

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

        if(savedInstanceState!=null){
            String message = savedInstanceState.getString("message");
            Toast.makeText(this, message, Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        outState.putString("message", "This is my message to be reloaded");
    }

我已经尝试在onSaveInstanceState()中切换2个语句,如下所示,但我得到了相同的结果。

@Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        outState.putString("message", "This is my message to be reloaded");
        super.onSaveInstanceState(outState, outPersistentState);
    }

1 个答案:

答案 0 :(得分:0)

您必须覆盖 PersistableBundle outPersistentState的其他方法。

   @Override
   public void onSaveInstanceState(Bundle outState) {
      super.onSaveInstanceState(outState);
      outState.putString("message", "This is my message to be reloaded");
   }