In my program, I start a thread and the progress dialog in the same time. I want to stop the thread after I pressed “Back”. But the program only stop my dialog box. I tried in my following code:
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
if (runner != null)
stopThread();
return true;
}
return super.dispatchKeyEvent(event);
}
@Override
public void onBackPressed(){
if (runner != null)
stopThread();
}
The result is I can only stop the thread when there are no any dialog box is still working, how can I “select” the back action or how can I stop the thread when the same time that dialog box dismiss?
Use
setOnCancelListenerfor the Dialog or if its a custom dialog then OverrideOnBackPressedand then stop the thread inside the method.