无法在 onSaveInstanceState 中捆绑数据

Unable to bundle data in onSaveInstanceState

我正在尝试保存以下数据:

 @Override
    public void setMessageDetailsExpanded(MessageHeaderItem i, boolean expanded,
            int heightBefore) {
        mDiff = (expanded ? 1 : -1) * Math.abs(i.getHeight() - heightBefore);
        String mHeaderItem = i.toString();
        final boolean  mDetailsExpanded = expanded;
        int mHeightBefore = heightBefore;
    }

在上面设置断点时,我看到了我想要捆绑的数据:

mMessageHeaderItem = com.android.mail.browse.ConversationViewAdapter$MessageHeaderItem@425a6f40

detailsExpanded = true 

heightBefore = 229

然而,当在 onSaveInstanceState 设置断点时 - 我的输出似乎不包含上述数据?!

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putFloat(BUNDLE_KEY_WEBVIEW_Y_PERCENT, calculateScrollYPercent());
    outState.putBoolean(getTag(), mDetailsExpanded);
    outState.putInt(getTag(),  height);
    outState.putString(getTag(), mHeaderItem);

}


Im not sure why but for some reason when debugging the code above it contains the following: 



 mDetailsExpanded = False

    height = 0 

    mHeaderItem = null 

我知道我忽略了一些小东西 - 但我似乎无法弄清楚为什么我没有将 setMessageDetailsExpanded 中的数据保存在我位于 onSavedInstanceState 的包中。

完整来源:

http://pastebin.com/Larzbwzh

第一期:您需要将要使用的变量声明为 class 变量

public class MyClass {
     private int x;
     private String y;
     ....

然后就可以在其他功能中正常访问了。

第二期:将需要的变量保存到bundle后调用super

outState.putString("myString", y);
super.onSaveInstanceState(outState) 

您的变量 mHeaderItem、mDetailsExpanded、mHeightBefore 似乎是本地变量/在 setMessageDetailsExpanded 中重新定义。因此在 onSaveInstanceState 中看不到分配的值。