savedInstanceState总是空 [英] savedInstanceState always null

查看:146
本文介绍了savedInstanceState总是空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个code在我OnsavedInstanceState

so i have this code in my OnsavedInstanceState

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    String [] a={"haha"};
    savedInstanceState.putStringArray("MyStringarray", a);
    Toast.makeText(context, "Saved array", Toast.LENGTH_SHORT).show();
}

和我有这个code在我的onCreate

and i have this code in my onCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if(savedInstanceState==null){
        Toast.makeText(this, "not there", Toast.LENGTH_SHORT).show();
    }else{
        Toast.makeText(this, "is there", Toast.LENGTH_SHORT).show();
    }
}

怎么来敬酒总是说不是吗?我打开应用程序,然后切换到另一个应用程序,它显示了敬酒保存阵列,但是当我重新打开应用程序它说,不存在即使包应该有包含字符串数组哈哈。

how come the toast always says not there? i opened the app then switched to another app and it showed the toast saved array but when i reopen the app it says not there even though the bundle should have the string array containing "haha".

非常感谢!

推荐答案

的onSaveInstanceState()您正在修改 savedInstanceState 和不保存修改后的对象。如果超级做的副本你的捆绑,那么将不保存此修改。

In onSaveInstanceState() you're modifying savedInstanceState and not saving this modified object. If super does a copy of your Bundle, then it will not save this modification.

尝试调用 super.onSaveInstanceState(savedInstanceState); 在替代方法结束

Try calling super.onSaveInstanceState(savedInstanceState); at the end of the method instead.

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    String [] a={"haha"};
    savedInstanceState.putStringArray("MyStringarray", a);
    super.onSaveInstanceState(savedInstanceState);
    Toast.makeText(context, "Saved array", Toast.LENGTH_SHORT).show();
}

这篇关于savedInstanceState总是空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆