2

The title says it all. How can I tell when onPause() is being called because of an orientation change instead of the back or home buttons being pressed?

John Roberts
  • 5,885
  • 21
  • 70
  • 124
  • if(isFinishing()) maybe but that works for onDestroy(). Try flagging if back was pressed. :) – Nikola Despotoski Oct 16 '13 at 23:01
  • why do you wanna tell onPause() called by different events? You just need to monitor different events to make your work. In onPause(), usually you just need to release resources and stop some services or thread. – yushulx Oct 17 '13 at 01:29

5 Answers5

1

I avoided the problem altogether by doing this instead:

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();
}
Community
  • 1
  • 1
John Roberts
  • 5,885
  • 21
  • 70
  • 124
  • onWindowFocusChanged is also called when you open popups (as far as I remember) and on Galaxy devices with multi Window when the user switches apps. This does not work in all situations. – meredrica Oct 19 '13 at 06:56
0

You cannot differentiate how onPause is called. What you could do is, You can override onKeyDown and look for back button and home button key events.

Have a flag that marks back/home button presses and check for this flag in onPause(). If this flag is not set, onPause is called cos of orientation change. However, there is a chance that your app is moved to background due to a phone call or alarm! So maynot be a perfect solution.

The other solution is to keep track of orientation changes in onConfigurationChanged

prijupaul
  • 2,076
  • 2
  • 15
  • 17
  • Back + Home button will not be enough. You can also leave the app via notifications, task switcher, other apps that open etc. `onConfigurationChanged` is not a good idea, see the discussion with @wtsang02 – meredrica Oct 17 '13 at 16:07
0

You probably want onSaveInstanceState instead of onPause

they serve similar function but onSaveInstanceState is not called when the user is backing out of the activity.

Pork 'n' Bunny
  • 6,740
  • 5
  • 25
  • 32
0

http://developer.android.com/reference/android/view/OrientationEventListener.html This should work. Why don't you toast in this.

-2

You can't. Simple as that. You can however measure the time it takes to reach onResume again and if the device was tilted by reading the display configuration.

An interval of 3 seconds is somewhat reasonable, also for slower devices.

Here are the relevant parts that we use:

protected void onCreate() {
  ...
  orientation = getResources().getConfiguration().orientation;
}

protected void onResume() {
  ...
  long time = SystemClock.elapsedRealtime() - pauseTime;
  int o = getResources().getConfiguration().orientation;
  Log.d(TAG, "pauseTime: " + pauseTime + " System: " + SystemClock.elapsedRealtime() + " elapsed pause time: " + time + " orientation now: " + o + " orientation then: " + orientation);
  if (time < 3000 && o != orientation) {
    // device was turned
  }
  orientation = o;
}

protected void onPause() {
  ...
  pauseTime = SystemClock.elapsedRealtime();
}

protected void onSaveInstanceState(final Bundle outState) {
  ...
  outState.putLong(PAUSE_TIME, pauseTime);
  outState.putInt(ORIENTATION, orientation);
}

protected void onRestoreInstanceState(final Bundle savedInstanceState) {
  ...
  pauseTime = savedInstanceState.getLong(PAUSE_TIME, -1);
  orientation = savedInstanceState.getInt(ORIENTATION, -1);
}

The above code is running on about 40k devices and reliably works.

meredrica
  • 2,563
  • 1
  • 21
  • 24