简体   繁体   English

Android-填充parentActivityName时,saveInstanceState为null

[英]Android - savedInstanceState null when parentActivityName filled

I have an Activity that call another one by calling startActivity() , after some time onSaveInstanceState(Bundle outState) is called and I set a Boolean valeu to recover on the onCreate(Bundle savedInstanceState) but when this method is called savedInstanceState comes null. 我有一个通过调用startActivity()调用另一个活动的Activity,在一段时间后调用了onSaveInstanceState(Bundle outState) ,并设置了一个布尔值来恢复onCreate(Bundle savedInstanceState)但是当调用此方法时, savedInstanceState null。 I've searched the internet for an answer, but didn't find anything for my case. 我已经在互联网上搜索了答案,但没有找到适合我的情况的内容。

Here's the onSaveInstanceState method: 这是onSaveInstanceState方法:

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState = new Bundle();
        outState.putBoolean(Constants.MAIN_ACTIVITY_STATE_RECREATED, true);
        super.onSaveInstanceState(outState);
    }

The onCreate : onCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setTitle(R.string.activity_main);
    if (savedInstanceState!=null && savedInstanceState.getBoolean(Constants.MAIN_ACTIVITY_STATE_RECREATED, false)){
        return;
    }
    //DO SOME STUFF
}

And here's my AndroidManifest declaration of the Activities: 这是我对Activity的AndroidManifest声明:

   <activity android:name=".MainActivity"
              android:screenOrientation="portrait"
              android:configChanges="orientation|keyboardHidden">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

This is the called Activity: 这就是所谓的活动:

       <activity android:name=".PhotoUserActivity"
                  android:screenOrientation="portrait"
                  android:configChanges="orientation|keyboardHidden"
            android:parentActivityName=".MainActivity">
        </activity>

If I remove parentActivityName from the declaration of the second activity and make the "back button" by myself, the onCreate of the first Activity is never called. 如果我从第二个活动的声明中删除parentActivityName,然后自己创建“后退按钮”,则永远不会调用第一个活动的onCreate。

Thanks for any help! 谢谢你的帮助!

You do not need to initialize the outState variable otherwise you wont be able to pass the vallue. 您不需要初始化outState变量,否则您将无法传递值。

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //outState = new Bundle(); <-- Remove this
        super.onSaveInstanceState(outState);
        outState.putBoolean(Constants.MAIN_ACTIVITY_STATE_RECREATED, true);
    }

The outState Bundle is provided by the framework, by initializing it eg new Bundle() you are potentially dropping some value on that previous Bundle if it contains any. outState Bundle是由框架提供的,通过对其进行初始化new Bundle()例如new Bundle() ,如果它包含任何先前的Bundle则可能会丢弃某些值。

It is always advisable to call the super.onSaveInstanceState(Bundle outState) first before adding new value. 始终建议在添加新值之前先调用super.onSaveInstanceState(Bundle outState)

If you save the state of the application in a bundle (typically non-persistent, dynamic data in onSaveInstanceState ), it can be passed back to onCreate if the activity needs to be recreated (eg, orientation change) so that you don't lose this prior information. 如果将应用程序的状态保存在捆绑包中(通常在onSaveInstanceState为非持久性动态数据),那么如果需要重新创建活动(例如,方向更改),则可以将其传递回onCreate ,以免丢失此先验信息。 If no data was supplied, savedInstanceState is null. 如果未提供任何数据,则savedInstanceState为null。

... you should use the onPause() method to write any persistent data (such as user edits) to storage. ...您应该使用onPause()方法将任何持久性数据(例如用户编辑)写入存储。 In addition, the method onSaveInstanceState(Bundle) is called before placing the activity in such a background state, allowing you to save away any dynamic instance state in your activity into the given Bundle, to be later received in onCreate(Bundle) if the activity needs to be re-created. 另外,在将活动置于这样的背景状态之前,将调用onSaveInstanceState(Bundle)方法,从而使您可以将活动中的任何动态实例状态保存到给定的Bundle中,如果该活动稍后在onCreate(Bundle)中接收需要重新创建。 See the Process Lifecycle section for more information on how the lifecycle of a process is tied to the activities it is hosting. 有关流程的生命周期如何与其托管的活动联系在一起的更多信息,请参见流程生命周期部分。 Note that it is important to save persistent data in onPause() instead of onSaveInstanceState(Bundle) because the latter is not part of the lifecycle callbacks, so will not be called in every situation as described in its documentation. 请注意,将持久性数据保存在onPause()中而不是onSaveInstanceState(Bundle)中非常重要,因为后者不是生命周期回调的一部分,因此不会在文档中描述的每种情况下都调用它。

source 资源

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

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