You can use onWindowFocusChanged event instead of onPause. This function is not called when orientation changed.
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
Log.d(TAG, "FOCUS = " + hasFocus);
if (!hasFocus) finish();
}
But note: this event is called when activity is still visible (like onPause()), you should use onStop if you want to finish the activity when it is really and fully invisible:
private boolean isInFocus = false;
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
Log.d(TAG, "FOCUS = " + hasFocus);
isInFocus = hasFocus;
}
@Override
public void onStop() {
super.onStop();
if (!isInFocus) finish();
}