我从蓝牙接收数据,然后我在TextView中显示它,并使用AChartEngine绘制它。 当我第一次开始活动时,情节显示正常。我还在通知(textview)中显示相同的数据。 当我按下HOME按钮时,我可以看到活动仍在运行,因为通知中的数据正在更新。但是当我回到活动时,图形将不会绘制,并且TextView保持为空(布局已重置)。
我已经阅读了一些有关此主题的内容,我相信每次都需要保存布局的状态。但我不知道如何为AChartEngine情节和textview做到这一点。
我读到我需要跟踪更改,并在每个更改中保存布局,例如onPause
或onStop
。我已经读过我需要使用onConfigurationChanged
。
有人可以向我解释如何在AChartEngine
和TextView
上保存状态吗?这一切都让我感到困惑,因为即使应用程序在后台运行时图形应该“更新”,当我恢复布局时,它是否会恢复图中的旧数据?或者它会显示最新数据吗?
由于
更新(1): 我添加了以下代码(使用示例@Dan建议):
@Override
protected void onSaveInstanceState(Bundle outState) {
//TODO
super.onSaveInstanceState(outState);
// save the current data, for instance when changing screen orientation
outState.putSerializable("datasetHR", mDatasetHR);
outState.putSerializable("datasetRR", mdatasetRR);
outState.putSerializable("datasetO2", mDatasetO2);
outState.putSerializable("rendererHR", mRendererHR);
outState.putSerializable("rendererRR", mRendererRR);
outState.putSerializable("rendererO2", mRendererO2);
outState.putSerializable("current_seriesHR", HRCurrentSeries);
outState.putSerializable("current_seriesRR", RRCurrentSeries);
outState.putSerializable("current_seriesO2", O2CurrentSeries);
outState.putSerializable("current_rendererHR", hrrenderer);
outState.putSerializable("current_rendererRR", rrrenderer);
outState.putSerializable("current_rendererO2", o2renderer);
outState.putSerializable("current_TV_HR", tvHR);
outState.putSerializable("current_TV_RR", tvRR);
outState.putSerializable("current_TV_O2", tvO2);
}
@Override
protected void onRestoreInstanceState(Bundle savedState) {
super.onRestoreInstanceState(savedState);
// restore the current data, for instance when changing the screen orientation
mDatasetHR = (XYMultipleSeriesDataset) savedState.getSerializable("datasetHR");
mdatasetRR = (XYMultipleSeriesDataset) savedState.getSerializable("datasetRR");
mDatasetO2 = (XYMultipleSeriesDataset) savedState.getSerializable("datasetO2");
mRendererHR = (XYMultipleSeriesRenderer) savedState.getSerializable("rendererHR");
mRendererRR = (XYMultipleSeriesRenderer) savedState.getSerializable("rendererRR");
mRendererO2 = (XYMultipleSeriesRenderer) savedState.getSerializable("rendererO2");
HRCurrentSeries = (XYSeries) savedState.getSerializable("current_seriesHR");
RRCurrentSeries = (XYSeries) savedState.getSerializable("current_seriesRR");
O2CurrentSeries = (XYSeries) savedState.getSerializable("current_seriesO2");
hrrenderer = (XYSeriesRenderer) savedState.getSerializable("current_rendererHR");
rrrenderer = (XYSeriesRenderer) savedState.getSerializable("current_rendererRR");
o2renderer = (XYSeriesRenderer) savedState.getSerializable("current_rendererO2");
tvHR = (TextView) savedState.getSerializable("current_TV_HR");
tvRR = (TextView) savedState.getSerializable("current_TV_RR");
tvO2 = (TextView) savedState.getSerializable("current_TV_O2");
}
但它仍然无济于事。 TextView和图形仍然恢复到其原始状态。问题远不止于此。我一直在从蓝牙接收数据。我在AChartEngine中绘制数据,并在TextView中显示它。活动在经过onPause
和onStop
后恢复后,数据不会更新。
当处理程序收到MESSAGE_READ
时它会更新,并且它会运行一个方法。在该方法中,我使用:
TextView tvHR = (TextView) findViewById(R.id.HRdisplay); // update the textview
TextView tvRR = (TextView) findViewById(R.id.RRdisplay); // update the textview
TextView tvO2 = (TextView) findViewById(R.id.O2display); // update the textview
tvHR.setText(hr);
tvRR.setText(rr);
tvO2.setText(o2);
将活动发送到后台(通过onPause
和onStop
后),然后重新开始,TextView将不会更新。但我知道数据进来了,它确实通过了上面的代码,因为同样的方法更新了Notifications,我可以看到那里的传入数据一直在更新。
更新#2:
我在onResume
中添加了以下代码:
if (savedInstanceState != null) {
//Then the application is being reloaded
onRestoreInstanceState(savedInstanceState);
}
但它仍未恢复到之前的状态,并且显示TextView和Graph仍未更新。
答案 0 :(得分:0)
您肯定需要覆盖onSaveInstanceState
和onRestoreInstanceState
,并存储和检索图表和textview值的数据集和渲染器。请参阅示例here。