javaandroidandroid-viewmodelchronometer

Chronometer is reset on when the app configuration changes in Android


I am created a Stopwatch app with using Chronometer and ViewModel the app is working well until the configuration change. when the rotation of the screen the Stopwatch is reset into 00:00.

this is part of StopwatchTabFrag.java.

stopwatchTabViewModel.buttonIcon.observe(getViewLifecycleOwner(), new Observer<Integer>() {
            @Override
            public void onChanged(Integer imgId) {

                switch (imgId){
                    //when the icon is changed to pause (or) when user clicks play icon then Stopwatch is start.
                    case R.drawable.ic_round_pause_circle_filled_24:
                        binding.stop.setVisibility(View.VISIBLE);
                        binding.startPause.setImageResource(imgId);
                        stopwatchTabViewModel.startStopwatch(stopwatchChronometer);
                        break;
                    //when the icon is changed to start (or) when user clicks pause icon then Stopwatch is pause.
                    case R.drawable.ic_round_play_arrow_24:
                        binding.startPause.setImageResource(imgId);
                        stopwatchTabViewModel.pauseStopwatch(stopwatchChronometer);
                        break;
                    case R.drawable.ic_round_stop_24:
                        binding.stop.setVisibility(View.GONE);
                        stopwatchTabViewModel.stopStopwatch(stopwatchChronometer);
                }
            }
        });

I am using Observer to whenever changing the particular button icon it was execute this particular task.

this is part of StopwatchTabViewModel.java.

public void startStopwatch(Chronometer chronometer){
        if (!isRunning.getValue()){
            chronometer.setBase(SystemClock.elapsedRealtime()-pauseOfset.getValue());
            chronometer.start();
            _isRunning.setValue(true);
        }
    }

    public void pauseStopwatch(Chronometer chronometer){
        if (isRunning.getValue()){
            chronometer.stop();
            _pauseOfset.setValue(SystemClock.elapsedRealtime()-chronometer.getBase());
            _isRunning.setValue(false);
        }
    }

    public void stopStopwatch(Chronometer chronometer){
        chronometer.setBase(SystemClock.elapsedRealtime());
        _pauseOfset.setValue(0L);
        chronometer.stop();

        //Reset the icon to Start
        _buttonIcon.setValue(R.drawable.ic_round_play_arrow_24);
    }

I implemented the Stopwatch task methods in ViewModel class. I don't know if this is correct or wrong.

I am waiting for your answers.


Solution

  • add this line to your Activitys declaration in manifest:

    android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"
    

    it will prevent Activity destroy on rotation (thus also your chrono), you have to handle rotation in onConfigurationChanged method call by yourself (you probably won't need to do anything in your case)

    HERE you have some info about configuration changing.