I have Chronometer in my app, that counts game elapsed time. I have save function on exit. I need to save time from chronometer, and to display same time when I load game after.
In save function I save chronometer.getBase() value. In load function I get that value and setBase() on chronometer. But that's not the right time, it seems that chronometer keeps ticking while app is closed.
How to do this? It seem that getBase isn't right value for my purpose.
I found a solution.
In the save function I store the value via this method:
private long calculateElapsedTime(Chronometer mChronometer) {
long stoppedMilliseconds = 0;
String chronoText = mChronometer.getText().toString();
String array[] = chronoText.split(":");
if (array.length == 2) {
stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 1000
+ Integer.parseInt(array[1]) * 1000;
} else if (array.length == 3) {
stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 60 * 1000
+ Integer.parseInt(array[1]) * 60 * 1000
+ Integer.parseInt(array[2]) * 1000;
}
return stoppedMilliseconds;
}
And in the load function I refresh the chronometer like this:
((GameActivity) activity).getcGameTime().setBase(
SystemClock.elapsedRealtime() - gameElapsedTime(getcGameTime()));
Hope this will help somebody someday. Cheers!