简体   繁体   English

我怎么知道什么时候由于方向改变而不是返回/返回而调用onPause()?

[英]How can I tell when onPause() is being called because of orientation change instead of back/home?

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? 我如何知道何时由于方向改变而不是按下后退或主页按钮而调用onPause()?

I avoided the problem altogether by doing this instead: 通过执行以下操作,我完全避免了该问题:

You can use onWindowFocusChanged event instead of onPause. 您可以使用onWindowFocusChanged事件代替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: 但请注意:当活动仍然可见时(例如onPause()),将调用此事件,如果要在完全不可见的活动结束时结束活动,则应使用onStop:

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();
}

You cannot differentiate how onPause is called. 您无法区分onPause的调用方式。 What you could do is, You can override onKeyDown and look for back button and home button key events. 您可以做的是,您可以覆盖onKeyDown并查找后退按钮和主页按钮的按键事件。

Have a flag that marks back/home button presses and check for this flag in onPause(). 有一个标志,用于标记按下后退/主页按钮,并在onPause()中检查该标志。 If this flag is not set, onPause is called cos of orientation change. 如果未设置此标志,则onPause称为方向改变的余弦。 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 另一个解决方案是在onConfigurationChanged跟踪方向更改

You probably want onSaveInstanceState instead of onPause 您可能需要onSaveInstanceState而不是onPause

they serve similar function but onSaveInstanceState is not called when the user is backing out of the activity. 它们提供类似的功能,但是当用户退出活动时不会调用onSaveInstanceState。

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. 但是,您可以通过读取显示配置来测量再次到达onResume所花费的时间以及设备是否倾斜。

An interval of 3 seconds is somewhat reasonable, also for slower devices. 对于较慢的设备,3秒的间隔还是比较合理的。

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. 上面的代码在大约40k的设备上运行,并且可靠地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM