Search code examples
javaandroidcountdowntimer

How To Destroy CoundownTimer in Java when the button is pressed?


I make a timer with a count time of 5 seconds, then when I press the exit button the counter automatically stops?

Here is my timer code:

    public void startTimer(final long finish, long tick) {
        CountDownTimer t;
        t = new CountDownTimer(finish, tick) {

            public void onTick(long millisUntilFinished) {
                long remainedSecs = millisUntilFinished / 1000;
                textTimer.setText("" + (remainedSecs / 60) + ":" + (remainedSecs % 60));// manage it accordign to you
            }

            public void onFinish() {
                textTimer.setText("00:00");
                Toast.makeText(FloatingVideoWidgetShowService.this, "Waktu Habis", Toast.LENGTH_SHORT).show();
                long seek = videoView.getCurrentPosition();
                videoView.setKeepScreenOn(false);
                stopSelf();
                WritableMap args = new Arguments().createMap();
                args.putInt("index", index);
                args.putInt("seek", (int) seek);
                args.putString("url", playingVideo.getString("url"));
                args.putString("type", "close");

                sendEvent(reactContext, "onClose", args);
                onDestroy();
                cancel();
            }
        }.start();

    }

And this is my code when pressing the stop / exit button :

        floatingWindow.findViewById(R.id.btn_deny).setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View view) {
                long seek = videoView.getCurrentPosition();
                videoView.setKeepScreenOn(false);
                stopSelf();
                WritableMap args = new Arguments().createMap();
                args.putInt("index", index);
                args.putInt("seek", (int) seek);
                args.putString("url", playingVideo.getString("url"));
                args.putString("type", "close");

                sendEvent(reactContext, "onClose", args);
                onDestroy();
            }
        });

How so when btn_deny is clicked Cuntdowntimer stops and does not force close?

Thanks.


Solution

  • You can't use onDestroy() to close your activity or fragment. Instead, you need to call finish().

    To close the CountDownTimer, you need to make it a class scope variable. Prepare the timer at your startTimer then stop the timer by calling t.cancel() like the following code:

    public class YourActivity extends Activity {
       // Declare the variable to be accessed later.
       CountDownTimer t;
    
       ...
    
       public void startTimer(final long finish, long tick) {
         t = new CountDownTimer(finish, tick) {
             ...
         }.start();
    
       }
    
    
       private void yourOtherMethod() {
    
        floatingWindow.findViewById(R.id.btn_deny).setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
              if(t != null) t.cancel();
              ...
           }
        });
       }
    
    }