2015-01-06 52 views
0

我試圖保存下列數據捆綁數據:無法的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 = co[email protected]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

回答

3

第一個問題:你需要聲明你想爲類變量

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

使用,那麼你可以正確地訪問他們在其他功能上的變量。

第二期:叫超級您保存您需要的變量後的束

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

你確定嗎?他們把之後(如本): http://sunil-android.blogspot.com/: http://pastebin.com/GW2VfkrR 似乎並沒有按照我所看到的每一個例子是正確的2013/03/save-and-restore-instance-state.html http://android-am.blogspot.com/2012/11/android-saved-instance-state-bundle.html –

+0

這裏是谷歌的例子,完成我建議的方式http://developer.android.com/training/basics/activity-lifecycle/recreating.html – Shooky

+0

謝謝!我很感激...我改變了我的代碼流以反映你的 - 但我仍然無法得到正確的數據值。一切總是返回mDetailsExpanded =假 高度= 0 mHeaderItem = NULL 當它應該是mMessageHeaderItem = co[email protected]425a6f40 detailsExpanded =真 heightBefore = 229 所以 - 我保存和恢復不正確的值:( 這是我的完整源(剛纔更新),如果有幫助: http://pastebin.com/Larzbwzh –

1

它看起來像你的變量mHeaderItem,mDetailsExpanded,mHeightBefore是本地/重新定義setMessageDetailsExpanded。所以在onSaveInstanceState中看不到分配的值。

+0

改正這個? –

+0

有什麼建議嗎? –

相關問題