0

I have a handler in android defined as this :

myHandler = new Handler(Looper.getMainLooper()){
            @Override
            public void handleMessage(Message msg){

                switch (msg.what){

                    case 1:

                        break;

                    case 5:

                        break;

                    case 4:

                        break;
                    case 3:

                        break;
                    default:
                        super.handleMessage(msg);
                }
            }
        };

Now what is the best way to kill clear this ? Should I use something as they do here: handler.removeCallbacksAndMessages(null); Is it better to do in in onStop() or onDestroy() ?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Trt Trt
  • 5,330
  • 13
  • 53
  • 86

1 Answers1

0

You can not stop a Runnable. You can just stop receiving Callbacks like this.

Handler handler = new Handler();
Runnable runnable = new Runnable() {
    @Override
    public void run() {
        Intent i = new
                Intent(MapsActivity.this,MapsActivity.class);
        startActivity(i);
        finish();
    }
};
handler.post(runnable);

// use this when you don't want callback to be called anymore
handler.removeCallbacks(runnable);
ziLk
  • 3,120
  • 21
  • 45
  • I am not using "handler.post(runnable);" in my code.. Should I? I am just sending messages from threads to the handlers – Trt Trt Jun 11 '18 at 14:56