java - 无法在 onSaveInstanceState 中 bundle 数据

标签 java android bundle scope instance-variables

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

 @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;
    }

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

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

最佳答案

第一个问题:您需要声明要用作类变量的变量

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

然后您就可以在其他功能中正确访问它们。

第二个问题:在包中保存所需的变量后调用 super

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

关于java - 无法在 onSaveInstanceState 中 bundle 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27806802/

相关文章:

java - 为什么不覆盖 Line2D.equals?

android - 如何在 Android 单元测试中打开控制台输出?

java - 如何在自定义ListView中使用Intent

php - 如何离线安装 DoctrineFixturesBundle

android - Intent 和 Bundle 的简单示例

java - UTF-8 不支持某些字符

java - org.hibernate.MappingException : Association references unmapped class

java - 在 Appfog 上部署简单的 Java Web 应用程序

Android:.nomedia 被忽略,图像仍然出现在图库中

javascript - 我如何准确了解 Webpack/Neutrino 正在开发中构建的内容?