1
@Override
public void onUserInteraction() {
    Handler handler = new Handler();
    handler.postDelayed(new Runnable () {
        public void run() {
            finish();
        }
    }, 5000);
    handler.removeCallbacks(null);
}

I am trying to run the handler when the OnCreate method is called. I would then like to cancel the handler and call it again (restart the handler) when I receive user interaction.

How would I do this?

Thanks in advance

hinch
  • 91
  • 2
  • 12

4 Answers4

6

You can use a boolean object, example:

@Override
public void onUserInteraction() {
    isStarted = true;
    Handler handler = new Handler();
    handler.postDelayed(new Runnable () {
        public void run() {
          if(isStarted){
            finish();
            }
         }
    }, 5000);

}

or you can make something like this:

Runnable myRunnable = new Runnable() {
    @Override
    public void run() {
        finish();
    }
};

public static Handler myHandler = new Handler();
private static final int TIME_TO_WAIT = 5000;

@Override
public void onUserInteraction() {
    restart();
}

public void start() {
    myHandler.postDelayed(myRunnable, TIME_TO_WAIT);
}

public void stop() {
    myHandler.removeCallbacks(myRunnable);
}

public void restart() {
    myHandler.removeCallbacks(myRunnable);
    myHandler.postDelayed(myRunnable, TIME_TO_WAIT);
}
Gevork Safaryan
  • 237
  • 1
  • 7
  • Thanks. This works for triggering the handler code but then how would i cancel it and call it again once i get another user interaction? Essentially I would like to close the app 5 seconds after I receive a user interact ion, but if there is another interaction before the 5 seconds then the timer will restart – hinch Sep 25 '17 at 10:38
  • I edit answer for you – Gevork Safaryan Sep 25 '17 at 10:40
  • Thankyou! (But there is no need for start or stop for my purpose so i just used restart) – hinch Sep 25 '17 at 10:57
2

Store the runnable and handler in a field and reuse them

Handler handler;
Runnable runnable = new Runnable () {
    public void run() {
        finish();
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    handler = new Handler();
    handler.postDelayed(runnable, 5000);
}

@Override
public void onUserInteraction() {
    handler.removeCallbacks(null);
    handler.postDelayed(runnable, 5000);
}
Tim
  • 41,901
  • 18
  • 127
  • 145
  • This partially works, however the userinteraction does not cancel the handler which is called oncreate. This means the app closes after 5 seconds even if the user is interacting with the app – hinch Sep 25 '17 at 10:47
  • @hinch I copied your call. Try with `handler.removeCallbacksAndMessages(null);` – Tim Sep 25 '17 at 10:54
0

Create a handler and keep its reference as global variable

private Runnable runnable;
private Handler newHandler;

newHandler = new Handler();
runnable = new Runnable() {
    @Override
    public void run() {
        try {
            //update UI
            newHandler.postDelayed(runnable,100);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
};
newHandler.post(runnable);

And on user Interaction

@Override
public void onUserInteraction() {
    super.onUserInteraction();
   newHandler.removeCallbacks(runnable);
    //Do some task
    if(//task done){
     newHandler.postDelayed(runnable,100);
     }
}
Sharath kumar
  • 4,064
  • 1
  • 14
  • 20
0

It was not work for me until I used Handler's removeCallbacksAndMessages(Object token) method.

On kotlin

private var restartingHandler = Handler()
private var runnable: () -> Unit = {}

fun start() {
    restartingHandler.postDelayed(runnable, 5000L)
}

fun restart() {
    restartingHandler.removeCallbacks(runnable)
    restartingHandler.removeCallbacksAndMessages(null)

    restartingHandler.postDelayed(runnable, 5000L)
}

fun stop() {
    restartingHandler.removeCallbacks(runnable)
    restartingHandler.removeCallbacksAndMessages(null)
}
Olga Konoreva
  • 1,338
  • 13
  • 20