1

I tried the same thing with the timer but still not working

Everytime I try to start the timer , it works but when I try to click again the button and cancel the countdown, it is not stopping or cancelling.

Here is my code:

public void timer(final Button btnBooking) {

    Hourglass hourglass = new Hourglass(5000, 1000) {
        @Override public void onTimerTick(long timeRemaining) { // Update UI
            btnBooking.setText("CANCEL " + "(" + timeRemaining + ")");
        }

        @Override public void onTimerFinish() { // Timer finished
            if (isBooking) {
                btnBooking.setText("CANCEL");
            }
            listener.startBooking();
        }
    };

    if (!isBooking) {
        hourglass.startTimer();
        isBooking = true;
    } else {
        isBooking = false;
        hourglass.stopTimer();
        btnBooking.setText("BOOK");
    }

}
Haem
  • 929
  • 6
  • 15
  • 31

2 Answers2

0

Every time you call the method timer, you create a new instance of the class Hourglass, and call startTimer or stopTimer on that. You need to store the hourglassyou call startTimer on outside the method, like in a field, and call stopTimer on that to get the expected behavior.

So, you'd do something like this:

private Hourglass hourglass;
public void timer(final Button btnBooking) {
    if (!isBooking) {
        hourglass = new Hourglass(5000, 1000) {
          @Override public void onTimerTick(long timeRemaining) { // Update UI
              btnBooking.setText("CANCEL " + "(" + timeRemaining + ")");
          }

          @Override public void onTimerFinish() { // Timer finished
              if (isBooking) {
                  btnBooking.setText("CANCEL");
              }
              listener.startBooking();
          }
        };
        hourglass.startTimer();
        isBooking = true;
    } else {
        isBooking = false;
        hourglass.stopTimer();
        btnBooking.setText("BOOK");
    }

}
Haem
  • 929
  • 6
  • 15
  • 31
0

Try this, might help you.

if (!isBooking) {
            hourglass = new Hourglass(5000, 1000) {
                @Override
                public void onTimerTick(long timeRemaining) { // Update UI
                    btnBooking.setText("CANCEL " + "(" + timeRemaining / 1000 + ")");
                }

                @Override
                public void onTimerFinish() { // Timer finished
                    if (isBooking) {
                        btnBooking.setText("CANCEL");
                        listener.startBooking();
                    }

                }
            };
            hourglass.startTimer();
            isBooking = true;
        } else {
            if (!hourglass.isRunning()) {
                dialogUtils.askDialog("Are you sure you want to cancel?", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        listener.stopBooking();
                        isBooking = false;
                        hourglass.stopTimer();
                        btnBooking.setText("BOOK");
                    }
                }, null);
            } else {
                isBooking = false;
                hourglass.stopTimer();
                btnBooking.setText("BOOK");
            }
        }
KikX
  • 217
  • 2
  • 17