I have an activity with override back button methods
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d(TAG, "onOptionsItemSelected");
if (item.getItemId() == android.R.id.home)
onBackPressed();
Log.d(TAG, "onOptionsItemSelected: if executed");
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
closeDialog();
}
It works fine to show Alert Dialog to the user on back button press from the hardware. When the user gives permission the activity is finished. But when the user clicks on the back arrow in the toolbar, the dialog is shown but the activities onPause(), onStop() methods are called and there is a window leak as the Alert Dialog is visible and activity is killed.
How to stop this issue?
NOTE: This does not happen when we don't provide parent activity in the manifest file!
You're supposed to return true
from onOptionsItemSelected
when you handle the press (inside your if
case).
Right now you're calling through to super.onOptionsItemSelected(item)
unconditionally which triggers activity finish on back press.