Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android thread still running after pressing back button

I want to stop a thread when a back button is pressed.
I'm using Handler.

like image 881
Siten Avatar asked Mar 23 '11 11:03

Siten


2 Answers

You need to use Handler's removeCallbacks() function.

Sample code:

@Override
public boolean onBackPress(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        handler.removeCallbacks(yourRunnable);
        return true;
    }

    return super.onKeyDown(keyCode, event);
}
like image 103
Wroclai Avatar answered Sep 27 '22 19:09

Wroclai


You can't use either stop or destroy methods. In the onStop method you have to use this.

like image 24
Udaykiran Avatar answered Sep 27 '22 19:09

Udaykiran