2014-04-11 76 views
1

我正在按方向更改序列化logCalculation方法对象。使用onSaveInstanceState序列化方向更改对象的正确方法是什么?

在方向更改上,logCalculation返回的值保存在Bundle outState中。所以它应该显示数据。

但是,在方向更改时,列表为空。它不是从savedInstanceState.getSerializable("calcLogs")的值

我在做什么错了?

这是我的代码 -

ArrayList logss = new ArrayList();

@Override 
    public void onSaveInstanceState(final Bundle outState) { 
     outState.putSerializable("calcLogs", calcLogs); 
    } 


if (savedInstanceState != null) { 
    try 
    { 
     calcLogs = (ArrayList<LogChartData>) savedInstanceState.getSerializable("calcLogs"); 
    } 
    catch(ClassCastException e) 
    { 
     e.printStackTrace(); 
    } 
} 


if(calcLogs == null) 
{ 
    calcLogs = logCalculation(Curcal.getTime()); 
} 

public ArrayList<LogChartData> logCalculation(final Date dt) { 

     getActivity().runOnUiThread(new Runnable() { 

     public void run() { 


     logss.clear(); 
     alert.clear(); 
     logss = fillChartData(dt); 

     // Some more data here.. 

     } 
    } 

     return logss; 
} 

回答

0

呼叫super.onSaveInstanceState(),以便根据需要

@Override 
    public void onSaveInstanceState(final Bundle outState) { 
     outState.putSerializable("calcLogs", calcLogs); 
     super.onSaveInstanceState(outState); 
    } 
+0

阿好抓的包被处理。必须在putSerializable之后调用它吗?序列化后调用它有什么不同? –

+0

是的,有必要先调用,因为您希望在处理之前添加数据。 –

+0

那么它仍然是空数据。这与“logCalculation”方法有关。这是一个先前没有任何返回的无效方法。我只返回logss。但是我可以看到它设置的更多变量。不能将void方法序列化,还是必须序列化它设置的变量? –

相关问题