我从蓝牙接收数据,然后将其显示在 TextView 中,并使用 AChartEngine 绘制它。当我第一次开始活动时,情节显示正常。我还在通知(文本视图)中显示相同的数据。当我按下 HOME 按钮时,我可以看到活动仍在运行,因为通知中的数据正在更新。但是当我回到活动时,图表不会绘制,并且 TextView 保持为空(布局已重置)。
我已经阅读了一些关于这个主题的内容,我相信我每次都需要保存布局的状态。但我不确定如何为 AChartEngine 绘图和 textview 执行此操作。
我读到我需要跟踪更改,并在每次更改时保存布局,例如onPause
或onStop
。我已阅读我需要使用onConfigurationChanged
.
谁能向我解释如何在AChartEngine
and上保存状态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
,数据不会被更新。
它在处理程序接收到 a 时更新MESSAGE_READ
,并运行一个方法。在该方法中,我使用以下方法更新 TextView:
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 不会更新。但我知道数据进来了,它确实通过了上面的代码,因为同样的方法更新了通知,我可以看到那里的传入数据一直在更新。
更新#2:
我在里面添加了以下代码onResume
:
if (savedInstanceState != null) {
//Then the application is being reloaded
onRestoreInstanceState(savedInstanceState);
}
但是依然没有恢复到之前的状态,显示的TextView和Graph依然没有更新。