Paul Alexander
Paul Alexander

Reputation: 2748

Android Chronometer starts and stops but carries on counting when stopped

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();
            }

        }

    });

Upvotes: 2

Views: 2347

Answers (1)

Giridharan
Giridharan

Reputation: 704

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

chronStopwatch.setBase(SystemClock.elapsedRealtime());

Upvotes: 10

Related Questions