I am receivng data from a bluetooth and then I display it in a TextView and also plot it using AChartEngine. The plot displays all right when I first start the activity. I also display the same data in a notification (textview). When I press the HOME button I can see that the activity is still running because the data in the notification is being updated. But when I go back to the activity, the graph won't plot, and TextView remains empty (the layout was reset).
I have read some on this topic, and i believe I need to save the state of the layout each time. But I am not sure how to do it for the AChartEngine plot and textview.
I read that I need to track the changes, and save the layout on each change like onPause
or onStop
. I have read that I need to use onConfigurationChanged
.
Could anyone please explain to me how to save the state on AChartEngine
and TextView
? It all confuses me, because the graph should be "updating" even when the application is running in the backgroud, and when I would restore the layout, will it restore the old data in the graph? or will it display the most recent data?
Thanks
UPDATE (1): I added the following code (using the sample @Dan suggested):
@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");
}
But it still does not help. The TextView and graphs are still restored to the their original state. The issue is more than that. I am constantly receiving data from the Bluetooth. I plot that data in the AChartEngine, and display it as it comes-in in the TextView. After the activity resumes after it went through onPause
and onStop
, the data is not being updated.
It updates when the handler receives a MESSAGE_READ
, and it runs a method. In that method I update the TextView using:
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);
After the activity is sent to background (goes through onPause
and onStop
), and then it resumes back, the TextView won't update. But I know that the data comes in, and it does go through that code above, because that same method updates the Notifications, and I can see the incoming data over there updates all the time.
Update #2:
I added the following code inside onResume
:
if (savedInstanceState != null) {
//Then the application is being reloaded
onRestoreInstanceState(savedInstanceState);
}
But still it is not restored to the previous state, and the display TextView and Graph are still not being updated.