Android Wear: How to implement voice action "Stop running"

119 views Asked by At

I want to implement voice actions, "start running" and "stop running".

"Start running" works fine, but "stop running" doesn't work.

My app has one activity with several fragments. When I speak "stop running", the activity is destroyed and created. My workout data is lost.

  • setRetainInstance(true) has no effect.
  • Change launchMode to singleTask/singleTop/singleInstance has no effect.
  • I saved workout data in onSaveInstanceState(), but it's lost when a new activity is created.

Is there any other way?

1

There are 1 answers

0
Alex K On

I'm not sure if this is too obvious, but have you considered using SharedPreferences?

To save data:

sPref = getPreferences(MODE_PRIVATE);
testNum = 2;
SharedPreferences.Editor editor = sPref.edit();
editor.putInt("testName", testNum);
editor.commit();

To get data:

sPref = getPreferences(MODE_PRIVATE);
int retrievedTestNum = sPref.getInt("testName",-1);
System.out.println("The number you saved was " + retrievedTestNum + "!");

You can also save data in arrays and store it in the SharePreferences.

If this is too simple for you, you can grab a mySQL database, which is much more robust.