Stop handler.postDelayed()

前端 未结 4 1431
被撕碎了的回忆
被撕碎了的回忆 2020-11-29 21:00

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

public class MyActivity extends AppCompatA         


        
4条回答
  •  无人及你
    2020-11-29 21:24

    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);
    

提交回复
热议问题