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

时间:2014-04-11 07:21:36

标签: android android-orientation

我在方向更改时序列化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;
}

1 个答案:

答案 0 :(得分:0)

调用super.onSaveInstanceState()以便根据需要处理该包

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