我在方向更改时序列化logCalculation
方法对象。
在方向更改时,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 :(得分:0)
调用super.onSaveInstanceState()
以便根据需要处理该包
@Override
public void onSaveInstanceState(final Bundle outState) {
outState.putSerializable("calcLogs", calcLogs);
super.onSaveInstanceState(outState);
}