In my android project i have an activity A which contains chronometer and a button to start it, a button to stop it, a button to pause it and everything is working fine. Now the problem is when i press the back button on my activity then chronometer time does not resume from the same state when i come back to that activity. I've tried many things many codes but nothing seems to work at all Mchronometrer.setbase(Mchronometer.getbase()b-SystemClock.elapsedRealTime()); Any suggestions or codes would be helpful
You have to save the state of your activity. Check this:.
To save your state use this:
private static final String CURRENT_TIME="current_time";
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the current time in millis
savedInstanceState.putLong(CURRENT_TIME, your_time_from_chronometer);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
and to restore the state
public void onRestoreInstanceState(Bundle savedInstanceState) {
// Always call the superclass so it can restore the view hierarchy
super.onRestoreInstanceState(savedInstanceState);
// Restore the time from saved
mCurrentTime = savedInstanceState.getInt(CURRENT_TIME);
}
It still depends in what format you have your time saved but a good options is using time in millis.