tccpg288
tccpg288

Reputation: 3352

How to remove a Runnable that is not assigned to a variable from a Handler

I started a runnnable but did not assign it to a variable. How do I stop it? I know it is a combination of the removeCallbacksAndMessages() method, but I do not know what parameter to pass in to this method since I created an anonymous runnable:

mStartButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            h = new Handler();
            final int delay = 1000; //milliseconds

            h.postDelayed(new Runnable() {
                public void run() {
                    mUpdateRef.addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            int mNumberOfPollsForCurrentDay = (int) dataSnapshot.getChildrenCount();
                            Random r = new Random();
                            int randomPollInRangeOfCurrentDayNumberOfPolls = r.nextInt((mNumberOfPollsForCurrentDay + 1) - 1) + 1;
                            int numberOfPollAnswersAtRandomNumber = (int) dataSnapshot.child(String.valueOf(randomPollInRangeOfCurrentDayNumberOfPolls)).child(POLL_ANSWERS_LABEL).getChildrenCount();
                            Random rr = new Random();
                            int randomAnswerBasedFromRandomPollAnswerChoices = rr.nextInt((numberOfPollAnswersAtRandomNumber + 1) - 1) + 1;
                            mUpdateRef.child(String.valueOf(randomPollInRangeOfCurrentDayNumberOfPolls)).child(POLL_ANSWERS_LABEL).child(String.valueOf(randomAnswerBasedFromRandomPollAnswerChoices)).child("Vote_Count").runTransaction(new Transaction.Handler() {
                                @Override
                                public Transaction.Result doTransaction(MutableData mutableData) {
                                    mutableData.setValue((Long) mutableData.getValue() + 1);
                                    return Transaction.success(mutableData);
                                }

                                @Override
                                public void onComplete(FirebaseError firebaseError, boolean b, DataSnapshot dataSnapshot) {

                                }
                            });

                        }

                        @Override
                        public void onCancelled(FirebaseError firebaseError) {

                        }
                    });
                    h.postDelayed(this, delay);
                }
            }, delay);

        }
    });

Upvotes: 2

Views: 258

Answers (1)

George Mulligan
George Mulligan

Reputation: 11923

Calling h.removeCallbacksAndMessages(null) will remove all callbacks and messages as stated in the documentation:

Remove any pending posts of callbacks and sent messages whose obj is token. If token is null, all callbacks and messages will be removed.

Note that this does not stop the runnable if it is already in the middle of executing but will remove it if it is still pending.

Upvotes: 5

Related Questions