Set a particular data on Firestore at a specific time using CountDownTimer

Viewed 223

Hello i want to set a particular data on firestore at a specific time using countDownTimer. When my countDown finish, the data is well set on firestore but each time the activity is update, the data is reset again and again. Please there is a possible way to destroy the finish method when my data is set once ?. if you have other suggestion to give your welcome

    firebaseAuth=FirebaseAuth.getInstance();
    firebaseFirestore=FirebaseFirestore.getInstance();

    long mil=200000;

    downTimer =new CountDownTimer(mil,1000) {
        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {

            Map<String,Object> map=new HashMap<>();
            map.put("Password","userPassword");

            firebaseFirestore.collection("UsersName").document().set(map).addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {

                    if (task.isSuccessful()) {

                        Toast.makeText(ForgetPasswordActivity.this, "Done sir", Toast.LENGTH_SHORT).show();
                       downTimer.cancel();
                    }
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {

                    e.printStackTrace();
                }
            });

        }
    }.start();

}

}

1 Answers

Every time your activity is restarted, a new instance of the timer is created and lives as long as your activity is alive.

If you need a single timer which will survive to activity lifecycle, you should put it in a foreground (or background) service or something similar.

Because you're using firebase, the other solution would be a cloud function.

Related