I have 3 Activities A,B & C.
I present some video links in Activity A in a list. If user clicks on an item, i load Activity B in which i do some verification of user’s data such as ask him to provide his Login info in an AlertDialog. I present two buttons in this dialog to Login & Cancel. If user presses Login i verify his info and open Activity C where i play his selected video. But If user presses Cancel, i finish() Activity B to load Activity A.
This works fine if user only interacts through burrons.
But Issue arises that when i press ESCAPE button on my keyboard/D-Pad when the AlertDialog is open. The dialog disappears but the Activity B does not finish(). I have overridden onKeyDown of Activity B where i do the following.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
|| keyCode == KeyEvent.KEYCODE_ESCAPE) {
Log.d(TAG, "onKeyDown : calling finish() manually");
finish();
return true;
} else
return false;
}
But it does not work WHEN i press ESCAPE on an open AlertDialog.
How can i achieve this?
Capture the setOnKeyListener event of AlertDialog and do paste the following code in it. It will also close the activity on BACK button: