android handler removeCallbacks not working
Asked Answered
A

4

5

i 'm rotating image with runnable.i want to rotate image for example 4 th time and then pause/stop rotate.i wrote some function

public void rotateImage(final View myView, final int size) {

    runnable = new Runnable() {

        @Override
        public void run() {

            count++;
            myView.setRotation(myView.getRotation() + size);
            if (count ==3) {
                myHandler.removeCallbacks(runnable);
            }

            myHandler.postDelayed(this, 100);
            // 1000 means 1 second duration
        }
    };
    myHandler.postDelayed(runnable, 100); 

}

i can rotate image but i can't stop/pause rotating .removeCallbacks not working at the moment what is a wrong in my code if anyone knows solution please help me

Aedes answered 24/10, 2014 at 10:19 Comment(1)
what if you add return; after myHandler.removeCallbacks(runnable);?Baiel
K
19

I had the same logic on my Handler/Runnable. And it was also not stopping.

What I did was: at my Activity's OnDestroy, I called

myHandler.removeCallbacksAndMessages(null);

And it finally stopped.

Hope it helps.

Kazan answered 16/7, 2015 at 5:47 Comment(1)
Thanks, I tried: myHandler.removeCallbacks(null); - Not worked.Substantive
H
3
/**
 * <p>Removes the specified Runnable from the message queue.</p>
 *
 * @param action The Runnable to remove from the message handling queue
 *
 * @return true if this view could ask the Handler to remove the Runnable,
 *         false otherwise. When the returned value is true, the Runnable
 *         may or may not have been actually removed from the message queue
 *         (for instance, if the Runnable was not in the queue already.)
 *
 * @see #post
 * @see #postDelayed
 * @see #postOnAnimation
 * @see #postOnAnimationDelayed
 */
public boolean removeCallbacks(Runnable action)

removeCallbacks() only works when the runnable is pendding in the message queue. The runnable you want to remove is obviously running.

You'd better stop it by yourself. Like:

public void rotateImage(final View myView, final int size) {

runnable = new Runnable() {

    @Override
    public void run() {

        count++;
        myView.setRotation(myView.getRotation() + size);
        if (count ==3) {
            //myHandler.removeCallbacks(runnable);
        } else {
            myHandler.postDelayed(this, 100);
        }
        // 1000 means 1 second duration
    }
};
myHandler.postDelayed(runnable, 100); 

}

Hybrid answered 8/6, 2016 at 7:9 Comment(0)
C
0

You have to move the creation of the Runnable from inside the function "rotateImage" to outside any function call. The removeCallbacks is working fine but your Runnable object is being recreated and although it has the same content the Id is different. This will work:

private Runnable runnable = new Runnable() {

        @Override
        public void run() {

            count++;
            myView.setRotation(myView.getRotation() + size);
            if (count ==3) {
                myHandler.removeCallbacks(runnable);
            }

            myHandler.postDelayed(this, 100);
            // 1000 means 1 second duration
        }
    };
public void rotateImage(final View myView, final int size) {

    myHandler.postDelayed(runnable, 100); 

}
Cephalochordate answered 24/7, 2019 at 17:46 Comment(0)
H
0

When removing the callback, pass the token as null, it worked here.

myHandler.removeCallbacks(runnable, null)
Habituate answered 13/10 at 2:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.