1

So my app has 1 activity and 4 fragments and one of them has Chronometer to show how much time is passed.

It works fine, but I have one issue that the chronometer keeps going back to 00:00 every time I move to another fragment and come back.

I know it is because my startStopWatch() method is in OnCreateView, but is there any way to make it continue from where it stopped?

Is it possible to use Bundle class to solve this? If so, how?

Here is my code for Chronometer, all are in one fragment:

public class FirstFragment extends Fragment {
    Chrhonometer chronometer;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_memo, container, false);
        chronometer = view.findViewById(R.id.stop_watch);
        chronometer.setFormat("%s");
        chronometer.setBase(SystemClock.elapsedRealtime());
        startStopWatch();
    }
}

And methods:

private void startStopWatch() {

    if (chronometer.getBase() != 0) {

        chronometer.setBase(SystemClock.elapsedRealtime());

    } else {

        chronometer.setBase(chronometer.getBase() + SystemClock.elapsedRealtime());

    }

    chronometer.start();

}

private void stopStopWatch() {

    chronometer.setBase(SystemClock.elapsedRealtime());
    chronometer.stop();

}

Thanks in advance.

Auclown
  • 179
  • 1
  • 3
  • 20

1 Answers1

1

Try this method

    public class FirstFragment extends Fragment {
            Chrhonometer chronometer;
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {
                final View view = inflater.inflate(R.layout.fragment_memo, container, false);
                chronometer = view.findViewById(R.id.stop_watch);
        ChronometerHelper chronometerHelper = new ChronometerHelper();
                startStopWatch();
            }

        private void startStopWatch() {
         if (chronometerHelper.getStartTime() == null) {
                    // If the start date is not defined, set it.
                    long startTime = SystemClock.elapsedRealtime();
                    chronometerHelper.setStartTime(startTime);
                    chronometer.setBase(startTime);
                } else {
                    // Otherwise set the chronometer's base to the original
                    // starting time.
                    chronometer.setBase(chronometerHelper.getStartTime());

                }

                chronometer.start();

        }

private void stopStopWatch() {

    long startTime = SystemClock.elapsedRealtime();
    chronometerHelper.setStartTime(startTime);
    chronometer.setBase(startTime);
    chronometer.stop();

}

        }

Create a class ChronometerHelper , in which you will save the starting time.

    public class ChronometerHelper {

    @Nullable
    private static Long mStartTime;

    @Nullable
    public Long getStartTime() {
        return mStartTime;
    }

    public void setStartTime(final long startTime) {
        this.mStartTime = startTime;
    }
}
Mushahid Gillani
  • 723
  • 7
  • 19
  • 1
    happy to help :) – Mushahid Gillani Oct 08 '18 at 07:55
  • Can you please help me one thing more? I also want to set the stopwatch to 00:00 when a button pressed, but how would i do that? I made a method called stopStopWatch() that has 2 lines of code which are chronometer.setBase(SystemClock.elapsedRealtime()); and chronometer.stop(); and call this method when the button is pressed but it seems it doesn't work. – Auclown Oct 08 '18 at 08:11
  • Thanks a lot again! – Auclown Oct 08 '18 at 08:44
  • Hey, I am sorry bothering you but I found my chronometer doesn't really stop but just go back to 00:00, can you help me with this? I have tried to solve this by myself but couldn't really find a solution. https://stackoverflow.com/questions/52699035/chronometer-timer-does-not-stop – Auclown Oct 08 '18 at 11:59