0

I have set up a callback to run every one second, I cannot seem to stop the process. I have asked this question on 4 different forums and no one can seem to answer this question it is very frustrating. Is there a way to stop this?

The method

   void startCallbackTimer() {

            callbackRunnable = new Runnable() {
                @RequiresApi(api = Build.VERSION_CODES.N)
                @Override
                public void run() {
                  
                    if (isIni&&dateSent&&UpdateSent) {
                        Result result = per.getResult();
                        Update(result);
                    }
                    callbackHandler.postDelayed(this, 1000);
                }
            };
            callbackHandler.postDelayed(callbackRunnable, 1000);

    }

I have tried to call multiple times:

callbackHandler.removeCallbacks(callbackRunnable);

But no luck, please can anyone answer this question i am stuck.

CHowsen
  • 25
  • 5
  • 4
    Does this answer your question? [How to remove all callbacks from a Handler?](https://stackoverflow.com/questions/5883635/how-to-remove-all-callbacks-from-a-handler) – Guillergood Jul 24 '20 at 07:00

1 Answers1

0

You should check that this is already answered...

You need to pass null to the parameter

callbackHandler.removeCallbacks(null)

If that does not work you can use

callbackHandler.removeCallbacksAndMessages(callbackRunnable) 
Guillergood
  • 76
  • 1
  • 8
  • Hi Thanks for the help, however that doesn't seem stop the call back firing, feel free to test it yourself but both options do not stop this nested post delayed call back it is very frustrating! – CHowsen Jul 24 '20 at 07:44
  • Yes, the thing is that everytime that you Runnable does something is posting again to do it. Maybe if you have some boolean to check your stop condition. If it needs to call again then do it, if not then call removeCallbacks. – Guillergood Jul 24 '20 at 08:15