简体   繁体   English

savedInstanceState始终为null

[英]savedInstanceState always null

so i have this code in my OnsavedInstanceState 所以我的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();
}

and i have this code in my onCreate 我在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". 我打开了该应用程序,然后切换到另一个应用程序,它显示了保存的吐司数组,但是当我重新打开该应用程序时,它说那里不存在,即使捆绑包中的字符串数组应包含“ haha​​”。

Many thanks! 非常感谢!

In onSaveInstanceState() you're modifying savedInstanceState and not saving this modified object. onSaveInstanceState()您正在修改savedInstanceState而不是保存此修改后的对象。 If super does a copy of your Bundle , then it will not save this modification. 如果super做了您Bundle的副本,那么它将不会保存此修改。

Try calling super.onSaveInstanceState(savedInstanceState); 尝试调用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();
}

The problem might be in how you have your activities defined in your manifest. 问题可能出在清单中如何定义活动。 For instance if your activity has the setting android:clearTaskOnLaunch="true" I don't think you will receive the saved bundle. 例如,如果您的活动设置为android:clearTaskOnLaunch =“ true”,我认为您不会收到保存的捆绑包。 See http://developer.android.com/guide/topics/manifest/activity-element.html for details on the various activity settings. 有关各种活动设置的详细信息,请参见http://developer.android.com/guide/topics/manifest/activity-element.html

You might also check the other overridden methods. 您可能还会检查其他替代方法。 For example in you override one and do something odd you could mess the activity stack up. 例如,在您覆盖一个并做一些奇怪的事情时,您可能会使活动堆栈混乱。 Do you call finish() anywhere in you code, if so remove it and see what happens. 您是否在代码中的任何地方调用finish(),如果要删除它,看看会发生什么。

Do not confuse this method with activity lifecycle callbacks such as onPause() , which is always called when an activity is being placed in the background or on its way to destruction , or onStop() which is called before destruction . 请勿将此方法与活动生命周期回调(例如onPause())混淆,该回调通常在将活动放置在后台销毁过程中时调用,或者在销毁之前调用onStop() One example of when onPause() and onStop() is called and not this method is when a user navigates back from activity B to activity A: there is no need to call onSaveInstanceState(Bundle) on B because that particular instance will never be restored, so the system avoids calling it. 何时调用onPause()和onStop()而不是使用此方法的一个示例是,当用户从活动B导航回活动A时: 无需在B上调用onSaveInstanceState(Bundle) ,因为该特定实例将永远不会还原,因此系统避免调用它。 An example when onPause() is called and not onSaveInstanceState(Bundle) is when activity B is launched in front of activity A: the system may avoid calling onSaveInstanceState(Bundle) on activity A if it isn't killed during the lifetime of B since the state of the user interface of A will stay intact. 调用onPause()不是onSaveInstanceState(Bundle)时的一个示例是在活动A之前启动活动B的情况:如果在活动B的生命周期内未被杀死,则系统可以避免在活动A上调用onSaveInstanceState(Bundle) A的用户界面状态将保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM