-7

From the documentation of the cancel method:

(Loosely speaking, this method returns true if it prevents one or more scheduled executions from taking place.)

And when I execute this code:

private Timer timer = null;
private TimerTask runnable = new TimerTask() {
    @Override
    public void run() {
        Log.e("DEBUG", "Scheduled task tick");
        if(!looper){
            Looper.prepare();
            looper = true;
        }
        VolleyConnect vc = new VolleyConnect();
        vc.connect(ErrorListActivity.this);
    }
};

@Override
public void onWindowFocusChanged(boolean focus){
    if(!focus){
        kill();
    }else{
        start();
    }
    Log.e("DEBUG", "FOCUS " + focus);
}

public void start(){
    if(timer == null){
        timer = new Timer();
        //180000 ms = 3 minutes
        timer.scheduleAtFixedRate(runnable, 0L, 180000L);<--- This line is the one that triggers the error.
    }
}

public void kill(){
    if(timer != null) {
        boolean rep = runnable.cancel();
        Log.e("DEBUG", "REP = " + rep);
        timer.cancel();
        timer = null;
        looper = false;
    }
}

Unexpectedly, the debug statement prints:

E/DEBUG: REP = true

which shows that the cancel should be completed. However, when I regain window focus (and try to reschedule the timer), the app crashes with this error:

java.lang.IllegalStateException: Task already scheduled or cancelled
    at java.util.Timer.sched(Timer.java:401)
    at java.util.Timer.scheduleAtFixedRate(Timer.java:328)
    at com.package.ErrorListActivity.start(ErrorListActivity.java:198)
    at com.package.ErrorListActivity.onWindowFocusChanged(ErrorListActivity.java:189)
    at android.support.v7.view.WindowCallbackWrapper.onWindowFocusChanged(WindowCallbackWrapper.java:128)
    at android.support.v7.view.WindowCallbackWrapper.onWindowFocusChanged(WindowCallbackWrapper.java:128)
    at com.android.internal.policy.DecorView.onWindowFocusChanged(DecorView.java:1414)
    at android.view.View.dispatchWindowFocusChanged(View.java:10173)
    at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1192)
    at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3757)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6120)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

I have no clue how to fix this, and I have looked through the other similar questions but nothing has worked yet. Any ideas?

Incidentally, this is not a duplicate of this question. It is the same issue, but I call cancel and it returns true (meaning it should work, but it doesn't).

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • 1
    Create a new `TimerTask` instance rather than trying to reuse an existing one. Or, switch to `ScheduledExecutorService`. – CommonsWare Apr 10 '17 at 13:38

2 Answers2

8

You can not reuse the timertask . Create new instance of TimerTask.

Krish
  • 3,860
  • 1
  • 19
  • 32
1

As Krish mentioned, you cannot reuse the TimerTask. If you want to, however, you can use Runnable instead of TimerTask and execute it with a ScheduledExecutorService. See this answer for an example.

Cardinal System
  • 2,749
  • 3
  • 21
  • 42