how to stop Handler in Android?

It sounds like the activity finishes its lifecycle before the Handler executes the code. You can manage a handler.post(runnable) by creating an instance member for the handler and runnable, then managing the handler in the Activity Lifecycle methods.

private Handler myHandler;
private Runnable myRunnable = new Runnable() {
    @Override
    public void run() {
        //Do Something
    }
};

Start the runnable with handler.post.

protected void onStart() {
    super.onStart();
    myHandler = new Handler();
    myHandler.post(myRunnable);

}

If the runnable hasn’t executed by the time onStop is called, we don’t want to run it.
Remove the callback in the onStop method:

protected void onStop() {
    super.onStop();
    mHandler.removeCallbacks(myRunnable);
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)