简体   繁体   English

android savedInstanceState始终为null

[英]android savedInstanceState always null

I have the following situation: 我有以下情况:

  • Activity A starts activity B 活动A开始活动B.
  • Activity A calls onPause , onSaveInstanceState and onStop methods. Activity A调用onPauseonSaveInstanceStateonStop方法。
  • On activity BI press the "UP button" on the action bar 在活动BI上,按操作栏上的“向上按钮”
  • Activity A first gets destroyed (calls onDestroy ) and then recreated. 活动第一个被破坏(调用onDestroy )然后重新创建。 (This just seems to happen this way. I have not implemented this way, just seems to be the Android way of doing things ..) (这似乎就是这样发生的。我没有实现这种方式,似乎只是Android的做事方式..)
  • During the onCreate method, the variable savedInstanceState is always null. onCreate方法期间,变量savedInstanceState始终为null。

I know there have been similar topics around here, but none of them seem to have an answer for my situation. 我知道这里有类似的话题,但他们似乎都没有答案我的情况。 All the callback methods have log lines in them, so I'm certain that the save method is executed and destory is executed. 所有的回调方法都有日志行,所以我确定执行了save方法并执行了destory。 But why is there never a savedInstanceState object? 但为什么还没有savedInstanceState对象?

My save method: 我的保存方法:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putSerializable(OpenSessionsActivity.SESSION, session);
    System.out.println("saving ..");
    super.onSaveInstanceState(savedInstanceState);
}

Is there any other code you need me to include? 你还需要我包含其他代码吗?

The top activity is recreated when navigating Up. 在向上导航时重新创建顶部活动。 To preserve state of the activity A you can 为了保持活动A的状态,你可以

A) Set launch mode of the activity A to "singleTop" (add android:launchMode="singleTop" to AndroidManifed.xml) A)将活动A的启动模式设置为“singleTop”(将android:launchMode="singleTop"到AndroidManifed.xml)

or 要么

B) Add FLAG_ACTIVITY_CLEAR_TOP flag to the up intent when navigating from the activity B: B)从活动B导航时, up意图添加FLAG_ACTIVITY_CLEAR_TOP标志:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            Intent up = NavUtils.getParentActivityIntent(this);
            up.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            NavUtils.navigateUpTo(this, up);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

This is a documented behavior: 这是一个记录在案的行为:

Similarly, if you navigate up to an activity on the current stack, the behavior is determined by the parent activity's launch mode. 同样,如果您向上导航到当前堆栈上的活动,则行为由父活动的启动模式确定。 If the parent activity has launch mode singleTop (or the up intent contains FLAG_ACTIVITY_CLEAR_TOP), the parent is brought to the top of the stack, and its state is preserved. 如果父活动具有启动模式singleTop(或up intent包含FLAG_ACTIVITY_CLEAR_TOP),则父项将被置于堆栈顶部,并保留其状态。 The navigation intent is received by the parent activity's onNewIntent() method. 导航意图由父活动的onNewIntent()方法接收。 If the parent activity has launch mode standard (and the up intent does not contain FLAG_ACTIVITY_CLEAR_TOP), the current activity and its parent are both popped off the stack, and a new instance of the parent activity is created to receive the navigation intent. 如果父活动具有启动模式标准(并且向上意图不包含FLAG_ACTIVITY_CLEAR_TOP),则当前活动及其父活动都从堆栈弹出,并且创建父活动的新实例以接收导航意图。

Press Up on the action bar is actually not the same thing as pressing the back button. 在操作栏上按“ Up ”实际上与按下back按钮不同。

If you want them to behave the same, you could use this code in Activity B: 如果您希望它们的行为相同,您可以在活动B中使用此代码:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

You should put the 你应该把

super.onSaveInstanceState(savedInstanceState);

before the code of saving your own data. 在保存自己的数据的代码之前。

As mentioned in the Documentation : 文档中所述

protected void onSaveInstanceState (Bundle outState): protected void onSaveInstanceState(Bundle outState):

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 ,系统可能会避免调用活动A上的onSaveInstanceState(Bundle) A的用户界面状态将保持不变。

If you do an orientation change with your device onStop() and onSavedInstanceState(...) will be called. 如果您使用设备onStop()进行方向更改,则会调用onSavedInstanceState(...)。

I had to add android:launchMode="singleTop" into the activity section of my manifest file to get this to work. 我不得不将android:launchMode="singleTop"到我的清单文件的activity部分中以使其工作。 I then only needed 然后我只需要

NavUtils.navigateUpFromSameTask(getActivity())

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

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