0

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?

4

1 回答 1

-1

我不确定这是否太明显,但您是否考虑过使用 SharedPreferences?

保存数据:

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

获取数据:

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

您还可以将数据保存在数组中并将其存储在 SharePreferences 中。

如果这对您来说太简单了,您可以使用更加健壮的 mySQL 数据库。

于 2014-11-27T21:40:50.987 回答