2

I've created an app that has a toggle button to start and stop a Chronometer, like a stop watch. When you first press the toggle button (executes .start()), the chronometer will start counting, when you then press the button again (executes .stop()), the chronometer appears on screen to stop counting as it should but when it is then started again it shows that it's actually still been counting in the background and shows the time on screen as if it was never stopped

code:

tglStartStop.setOnCheckedChangeListener(new OnCheckedChangeListener(){

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {

            if (tglStartStop.isChecked()) {

                chronStopwatch.start();
            }

            else {

                chronStopwatch.stop();
            }

        }

    });
Squonk
  • 48,735
  • 19
  • 103
  • 135
Paul Alexander
  • 2,686
  • 4
  • 33
  • 69
  • 1
    Just a guess but it seems like you should call `setBase(...)` immediately before calling `start()`. The reason is that `start()` and `stop()` don't change the 'base' time originally set by the `setBase(...)` method. In other words, the `Chronometer` doesn't actually keep counting after it is stopped but if it's stopped / restarted the the count will be relative to the original base time. – Squonk Jan 09 '15 at 11:35

1 Answers1

10

Add this line before "chronStopwatch.start();"

chronStopwatch.setBase(SystemClock.elapsedRealtime());
Giridharan
  • 708
  • 7
  • 16