2

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.

KingsInnerSoul
  • 1,373
  • 4
  • 20
  • 49

1 Answers1

0

You definitely need to override onSaveInstanceState and onRestoreInstanceState and store and retrieve the dataset and the renderer for the chart and the textview value. Please see an example here.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
  • 1
    I have added both `onSaveInstanceState` and `onRestoreInstanceState` but it still does not help me. It seems just to ignore it. But, what confuses me the most is the fact that the incoming data is being shown and updated in the notification's textview, but when i return to the activity after it was set to the background, the textview and graph are initialized and empty. And the data is still shown in the notification's textview, but not updated in the activity's main.xml textview. – KingsInnerSoul Feb 21 '13 at 16:01
  • 1
    Yes, I did. I updated the question, so you can see what I have done so far. – KingsInnerSoul Feb 21 '13 at 17:10
  • 1
    Take a look at onResume now and do something similar. – Dan D. Feb 21 '13 at 17:15
  • 1
    Hi @Dan, I added update in the original question. I added a small snippet of code in the onResume. Am I still doing it incorrect? – KingsInnerSoul Feb 21 '13 at 17:31
  • Please type AChartEngine correctly. You mistyped it in two places. – Dan D. Feb 21 '13 at 17:48
  • Under onResume, you must repaint the chart, not the thing you are doing. Take a look at the example I mentioned. – Dan D. Feb 21 '13 at 17:49
  • I will accept your answer, but after looking back into my application, I was using an Intent to call a the same (i guess new?) activity, and it was all differnet. I used `Intent i = new Intent(this, MyMainActivity.class); i.setAction(Intent.ACTION_MAIN); i.addCategory(Intent.CATEGORY_LAUNCHER); startActivity(i);` for calling the same activity back to the foreground. I found this here: http://stackoverflow.com/questions/2232238/how-to-bring-an-activity-to-foreground-top-of-stack – KingsInnerSoul Feb 24 '13 at 15:06