Search code examples
javaandroidfirebasefirebase-realtime-databasecountdowntimer

CountDownTimer cancel() doesn't cancel the timer and continue working


I'm using CountDownTimer class to update firebase realtime database, but cancel() method seems doesn't work.

I want when the timer finishes to call fetchUserPoints() and to add the points itself.

Could you help me, please?

int time = 6;
private CountDownTimer countDownTimer;


private void timeDownTimer() {
        countDownTimer = new CountDownTimer(6000, 1000) {

            public void onTick(long millisUntilFinished) {
                tvTimer.setText("0:" + checkDigit(time));
                time--;

            }

            public void onFinish() {
                tvTimer.setText("Time finished");
                fechUserPoints();
            }


        }.start();
    }

    public String checkDigit(int number) {
        return number <= 9 ? "0" + number : String.valueOf(number);
    }

    private void fetchUserPoints() {
        countDownTimer.cancel();
        reference.child(mId).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                prevPoints = dataSnapshot.child("points").getValue().toString();
                int prePoints = Integer.valueOf(prevPoints);
                String newPoints = String.valueOf(prePoints + ADDED_POINTS);

                reference.child(mId).child("points").setValue(newPoints).addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Toast.makeText(getContext(), "Process is successful", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

and here fetchUserPoints() continue calling.


Solution

  • You can just cancel your CountDownTimer inside onFinish method. Try this

    int time = 6;
    private CountDownTimer countDownTimer;
    
    
    private void timeDownTimer() {
            countDownTimer = new CountDownTimer(6000, 1000) {
    
                public void onTick(long millisUntilFinished) {
                    tvTimer.setText("0:" + checkDigit(time));
                    time--;
    
                }
    
                public void onFinish() {
                    tvTimer.setText("Time finished");
                    countDownTimer.cancel();
                    fechUserPoints();
                }
    
    
            }.start();
        }
    
        public String checkDigit(int number) {
            return number <= 9 ? "0" + number : String.valueOf(number);
        }
    
        private void fetchUserPoints() {
    
            reference.child(mId).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    prevPoints = dataSnapshot.child("points").getValue().toString();
                    int prePoints = Integer.valueOf(prevPoints);
                    String newPoints = String.valueOf(prePoints + ADDED_POINTS);
    
                    reference.child(mId).child("points").setValue(newPoints).addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {
                            Toast.makeText(getContext(), "Process is successful", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
    
                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
    
                }
            });
        }
    

    Another example see here it working

    private CountDownTimer countDownTimer;
        int time = 6;
    
        private void timeDownTimer() {
            countDownTimer = new CountDownTimer(6000, 1000) {
    
                public void onTick(long millisUntilFinished) {
                    Log.e("TIK : " , checkDigit(time));
                    time--;
    
                }
    
                public void onFinish() {
                    countDownTimer.cancel();
                    test();
                }
    
    
            }.start();
        }
    
        public String checkDigit(int number) {
            return number <= 9 ? "0" + number : String.valueOf(number);
        }
    
        private void test(){
            Log.e("TIK : " , "inside my method");
            Toast.makeText(this,"on finish",Toast.LENGTH_LONG).show();
        }