调用完成()内的onPause(),而不是在方向更改 [英] Call finish() inside onPause() but not when orientation changes

查看:122
本文介绍了调用完成()内的onPause(),而不是在方向更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序需要调用结束时,有人退出它的主要活动(所以我不希望它是暂停),即使是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.

你知道吗?

推荐答案

您可以使用<一个href="http://developer.android.com/reference/android/app/Activity.html#onWindowFocusChanged%28boolean%29">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();
}

这篇关于调用完成()内的onPause(),而不是在方向更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆