Hi I recently have made a simple stopwatch app, it work fine. However when I switch the screen of and turn it back on the timer shows the last value of time. For example if the timer was showing me 00:00:10 , turn the screen of (for 5s) turn it back on it and it will show same value and the starts to increase the time value.
Any suggestion why ?
Thanks
if (savedInstanceState != null) {
seconds = savedInstanceState.getInt("seconds");
running = savedInstanceState.getBoolean("running");
wasRunning = savedInstanceState.getBoolean("wasRunning");
}
runTimer();
}
public void onClickStart(View view){
running = true;
}
public void onClickStop(View view){
running = false;
}
public void onClickReset(View view){
running = false;
seconds = 0;
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putInt("seconds", seconds);
savedInstanceState.putBoolean("running", running);
savedInstanceState.putBoolean("wasRunning",wasRunning);
}
@Override
protected void onPause(){
super.onPause();
wasRunning = running;
running = false;
}
@Override
protected void onResume(){
super.onResume();
if(wasRunning){
running = true;
}
}
@Override
protected void onStop(){
super.onStop();
wasRunning = running;
running = false;
}
@Override
protected void onStart(){
super.onStart();
if(wasRunning){
running = true;
}
}
private void runTimer(){
final TextView timeView = (TextView)findViewById(R.id.time_view);
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
int hours = seconds/3600;
int minutes = (seconds%3600)/60;
int secs = seconds%60;
String time = String.format("%d:%02d:%02d",hours,minutes,secs);
timeView.setText(time);
if(running){
seconds++;
}
handler.postDelayed(this,1000);
}
});
}