调用完成()内的onPause(),而不是在方向更改是在、而不、方向、onPause

由网友(寂笙)分享简介:我有一个应用程序需要调用结束时,有人退出它的主要活动(所以我不希望它是暂停),即使是pressing家活动已经完成,处理这个目前我只需要调用完成()在我的的onPause()方法,因为一切都与片段做了工程pretty的很好,没有给出稳定性问题。I have an app that needs to call fini...

我有一个应用程序需要调用结束时,有人退出它的主要活动(所以我不希望它是暂停),即使是pressing家活动已经完成,处理这个目前我只需要调用完成()在我的的onPause()方法,因为一切都与片段做了工程pretty的很好,没有给出稳定性问题。

I have an app that needs to call finish when someone exits its main activity (so i do not want it to be paused), even by pressing home activity has to be finished, to handle this currently i simply call finish() in my onPause() method, since everything is done with fragments it works pretty well and gives no stability issues.

我唯一的问题是,我无法处理,因为的onPause()之前 onConfigurationChanged()称为方向变化(让我为禁用旋转时出现这种情况)。

My only problem is that i cannot handle orientation changes since onPause() is called before onConfigurationChanged() (allowing me to disable this behavior when rotation occurs).

我可以创建一个处理这样的服务,但它的方式复杂。

I could create a service that handles this but its way to complex.

你知道吗?

推荐答案

您可以使用onWindowFocusChanged事件,而不是在onPause。此功能不叫时的方向改变。

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

但要注意:此事件被称为活动时仍然可见(象的onPause()),你应该 使用的onStop,如果你想完成的活动时,它是真正,完全看不见的:

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();
}
阅读全文

相关推荐

最新文章