Stop handler.postDelayed()
Asked Answered
D

6

143

I call multiple Handlers by new Handler().postDelayed(new Runnable()..... How can I stop it when I click on back?

public class MyActivity extends AppCompatActivity implements OnClickListener {

    private Button btn;
    private Handler handler;
    private Runnable myRunnable;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        btn = (Button) findViewById(R.id.trainingsstart);
        btn.setOnClickListener(this);   
    }

    @Override
    public void onClick(View v) {
        Handler handler =  new Handler();
        Runnable myRunnable = new Runnable() {
            public void run() {
                // Things to be done
            }
        };

        handler.postDelayed(myRunnable, 3000);
    }

    @Override 
    public void onBackPressed() { 
        super.onBackPressed();
        handler.removeCallbacks(myRunnable);
    }   
}

I have the problem that my Handlers haven't a name like "myHandler". How can I handle it?

Thanks for help!

Now I got these error:

03-28 17:47:03.662: E/AndroidRuntime(1961): FATAL EXCEPTION: main
03-28 17:47:03.662: E/AndroidRuntime(1961): java.lang.NullPointerException
03-28 17:47:03.662: E/AndroidRuntime(1961):     at de.basti12354.tage.uebungen.Tag1.onBackPressed(Tag1.java:120)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.app.Activity.onKeyUp(Activity.java:2159)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.KeyEvent.dispatch(KeyEvent.java:2647)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.app.Activity.dispatchKeyEvent(Activity.java:2389)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1860)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3791)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3774)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3516)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3666)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:1982)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1698)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1689)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1959)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.os.MessageQueue.nativePollOnce(Native Method)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.os.MessageQueue.next(MessageQueue.java:132)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.os.Looper.loop(Looper.java:124)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.app.ActivityThread.main(ActivityThread.java:5103)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at java.lang.reflect.Method.invokeNative(Native Method)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at java.lang.reflect.Method.invoke(Method.java:525)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at dalvik.system.NativeStart.main(Native Method)
Dyann answered 28/3, 2014 at 17:0 Comment(0)
S
253

You can use:

 Handler handler = new Handler()
 handler.postDelayed(new Runnable())

Or you can use:

 handler.removeCallbacksAndMessages(null);

Docs

public final void removeCallbacksAndMessages (Object token)

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

Or you could also do like the following:

Handler handler =  new Handler()
Runnable myRunnable = new Runnable() {
public void run() {
    // do something
}
};
handler.postDelayed(myRunnable,zeit_dauer2);

Then:

handler.removeCallbacks(myRunnable);

Docs

public final void removeCallbacks (Runnable r)

Added in API level 1 Remove any pending posts of Runnable r that are in the message queue.

public final void removeCallbacks (Runnable r, Object token)

Edit:

Change this:

@Override
public void onClick(View v) {
    Handler handler =  new Handler();
    Runnable myRunnable = new Runnable() {

To:

@Override
public void onClick(View v) {
    handler = new Handler();
    myRunnable = new Runnable() { /* ... */}

Because you have the below. Declared before onCreate but you re-declared and then initialized it in onClick leading to a NPE.

Handler handler; // declared before onCreate
Runnable myRunnable;
Somnambulism answered 28/3, 2014 at 17:5 Comment(6)
Have add the handler.removeCallbacks(myRunnable); to my onBackPressed() is that right?Dyann
@user3433232 use it you want to stop the runSomnambulism
Can you please look at my code, I try to use your help´, but now I got the errors, you can see at my post.Dyann
@user3433232 check the edit. Your Handler was null. You re -declared it in onClick leading to NPESomnambulism
I am working on API 16 and handler.removeCallbacks(runnable) doesn't work. But handler.removeCallbacksAndMessages(null); works thanksHumanity
Thanks ! In my case following code works perfectly.handler.removeCallbacksAndMessages(null); I have put that code in fragment's onPause() method and in onDestroy() method.Pediatrician
B
23

this may be old, but for those looking for answer you can use this...

public void stopHandler() {
   handler.removeMessages(0);
}

cheers

Buck answered 14/3, 2015 at 16:22 Comment(0)
S
21

You can define a boolean and change it to false when you want to stop handler. Like this..

boolean stop = false;

handler.postDelayed(new Runnable() {
    @Override
    public void run() {

        //do your work here..

        if (!stop) {
            handler.postDelayed(this, delay);
        }
    }
}, delay);
Sealskin answered 5/4, 2017 at 17:41 Comment(0)
F
2
  Boolean condition=false;  //Instance variable declaration.

 //-----------------Inside oncreate--------------------------------------------------- 
  start =(Button)findViewById(R.id.id_start);
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                starthandler();

                if(condition=true)
                {
                    condition=false;
                }


            }
        });

        stop=(Button) findViewById(R.id.id_stoplocatingsmartplug);

        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stophandler();

            }
        });


    }

//-----------------Inside oncreate---------------------------------------------------

 public void starthandler()
    {

        handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {


                if(!condition)
                {
                    //Do something after 100ms 


                }

            }
        }, 5000);

    }


    public void stophandler()
    {
        condition=true;
    }
Fregger answered 22/2, 2018 at 12:34 Comment(3)
Please explain why this piece of code answers the question.Lozier
Assume u have 2 Button in XML start and Stop. This code shows the demo of how the handler can be started withe the click of start button and can be stopped by stop button.Fregger
It works but the runnable still runs in the background.Steeplebush
K
1

Explanation:

Declaring the specific handler before 'onCreate' is necessary. If it is declared inside 'onCreate', they are not sharing the same declaration and thus cannot be identified. This is the problem that the author faced. He has accidentally declared the specific handler inside 'onCreate'.

Solution:

public class MyActivity extends AppCompatActivity implements OnClickListener {

    private Button btn;
    private Handler handler;
    private Runnable myRunnable;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        btn = (Button) findViewById(R.id.trainingsstart);
        btn.setOnClickListener(this);   
    }

    @Override
    public void onClick(View v) {
        handler =  new Handler();
        myRunnable = new Runnable() {
            public void run() {
                // Things to be done
            }
        };

        handler.postDelayed(myRunnable, 3000);
    }

    @Override 
    public void onBackPressed() { 
        super.onBackPressed();
        handler.removeMessages(0);
    }   
}
Kolo answered 4/2, 2022 at 15:43 Comment(0)
R
0

The best option is to use a boolean as a flag like that, use boolean in onPause & onResume

TIME_OUT = 45000;

//add this boolean
boolean run =true;

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {

            //run this method only when run is true
             if(run==true){

                //your code
               }

        }
    }, TIME_OUT);

         //change values in onPause
          run=false;
Reichel answered 20/7, 2022 at 5:58 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.