2

I want to set dynamically auto scroll speed to WebView. In onCreate calling autoScroll(25) and remove it, nextly calling autoScroll(300) but when the apk is running the auto scroll speed is 25 so earlier called 'mHandler.postDelayed' do not removing. How to fix the problem?

Handler mHandler;
Runnable runnable;
WebView wv;

protected void onCreate(Bundle savedInstanceState) {
    ...
    autoScroll(25);
    mHandler.removeCallbacks(runnable);
    autoScroll(300);
}

public void autoScroll(final int speed){
    if(mHandler == null) {
        mHandler = new Handler();
    }
    wv.post(runnable = new Runnable() {
        @Override
        public void run() {
            wv.scrollBy(0, 1);
            mHandler.postDelayed(this, speed);
        }
    });
}
ATES
  • 281
  • 1
  • 11
  • 29

1 Answers1

1
mHandler.removeCallbacks(runnable);

will only remove any pending posts of Runnable r that are in the message queue. It will not stop an already running thread. You have to explicitly stop the thread. One way to stop that thread is to use a boolean variable as a flag and run your code inside runnable based on the value of that flag. You can take some hints from https://stackoverflow.com/a/5844433/1320616

Community
  • 1
  • 1
Ankit Aggarwal
  • 5,317
  • 2
  • 30
  • 53