You can use the onWindowFocusChanged event instead of onPause. This function is not called when the orientation changes.
@Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); Log.d(TAG, "FOCUS = " + hasFocus); if (!hasFocus) finish(); }
But note: this event is fired when the activity is still visible (for example, onPause ()), you should use onStop if you want to end the action when it is really and completely 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(); }
source share