0

I modified the BLE app so that it plots data that's coming from a sensor, shown below: enter image description here

Now, when I close the app I want to be able to save the plot, so that next time when I open the app again, I can continue plot the graph instead of starting over.

I googled around and found this person recommending using Icepick library to save the app state. I tried his method but it didn't work, maybe I am using it wrong.

I added the @state variables at the top, added the @overide function and added the restoreInstanceState() function in my onCreate. Then I tried to store the last x and y value into the @state variables that I declared at the top of my code. Following is my code structure in DeviceControlActivity.java.

public class DeviceControlActivity extends Activity {
    ...
    @State int saved_x_axies = 0;
    @State int saved_y_axies = 0;
    ...
   @Override public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Icepick.saveInstanceState(this, outState);
   }

   ...
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.button_control);
      Icepick.restoreInstanceState(this, savedInstanceState);
      // double y, x;
     Log.d("array","array ---> " + saved_y_axies);
     ...
   }
   ...
       private void displayData(TextView dataField, String data) {
    if (data != null) {
        dataField.setText(data);
        if(dataField == ch1DataField) {
            String[] parts = data.split(" ");
            int ch1data =  Integer.parseInt(parts[1]);
            series0.appendData(new DataPoint(X_axies++, ch1data), true, 33);
            saved_y_axies = ch1data;
            saved_x_axies = X_axies;
        }
    }
}

}

Question 1, saved_y_axies gives me zero when I print it out using Log.d. From my code structure, is this how I am suppose to use the icepick library, if not can anyone tell me what I am doing wrong?

Question 2, can icepick library store int arrays? ultimately, I am thinking of storing all the sensor data into an array, when the app opens again, I am going to re-plot all the values to get the original graph back.

Thanks for the help!

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
BOB
  • 151
  • 3
  • 13
  • If you are closing your app then how it maintain your state, You should use shared preference or database for such – Jitesh Mohite Sep 07 '17 at 03:11
  • This is an everyday Android question that has been answered here *many* times before. – Chris Stratton Sep 08 '17 at 03:35
  • https://stackoverflow.com/a/18463758/5186878 https://stackoverflow.com/questions/27624068/app-is-crashing-with-shared-preferences these two links saved me – BOB Sep 08 '17 at 03:59

1 Answers1

0

Ended up using SharedPreferences to save and get values:

global variable:

public SharedPreferences pref;

inside oncreate():

pref = getSharedPreferences("MyPrefs",Context.MODE_PRIVATE);

     //saves the array using shared preference key, value pairs
    SharedPreferences.Editor prefsEditor = pref.edit();
    prefsEditor.putString("ARRAY_KEY_MID", json);
    prefsEditor.putString("ARRAY_KEY_AIR", json_air);
    prefsEditor.putString("ARRAY_KEY_DEEP", json_deep);
    //saves the x axeies
    prefsEditor.putInt("INT_KEY", X_axies);
    Log.d("close", "array------> " + json);
    prefsEditor.commit();


    ////retrive all the values using the keys
    SharedPreferences.Editor editor_clear = pref.edit();
    X_axies = pref.getInt("INT_KEY", 0);
    String json = pref.getString("ARRAY_KEY_MID", "");
    String json_air = pref.getString("ARRAY_KEY_AIR", "");
    String json_deep = pref.getString("ARRAY_KEY_DEEP", "");
BOB
  • 151
  • 3
  • 13