Restart a handler.postDelayed in Java [duplicate]
Asked Answered
F

4

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

Fling answered 25/9, 2017 at 10:32 Comment(1)
Check developer.android.com/reference/android/os/…Stomatology
M
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);
}
Midian answered 25/9, 2017 at 10:35 Comment(3)
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 restartFling
I edit answer for youMidian
Thankyou! (But there is no need for start or stop for my purpose so i just used restart)Fling
F
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);
}
Foolproof answered 25/9, 2017 at 10:36 Comment(2)
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 appFling
@Fling I copied your call. Try with handler.removeCallbacksAndMessages(null);Foolproof
L
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);
     }
}
Leatherworker answered 25/9, 2017 at 10:43 Comment(3)
It says newHandler and runnable are not assignedFling
where you are getting that error?in the beginning or inside userinteration method?Leatherworker
its working code..it should work fine,any ways updated code..check nowLeatherworker
D
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)
}
Dulci answered 23/8, 2018 at 10:50 Comment(1)

© 2022 - 2024 — McMap. All rights reserved.