onConfigurationChanged 有条件地重新启动 Activity
我想做一个允许在某些条件下改变方向的活动,但在其他情况下不允许。更确切地说,我想防止在后台线程繁忙时重新启动活动。
我已将 configChanges
属性放在活动清单上,并在方向更改时调用 onConfigurationChanged
。不过,我希望允许应用在允许时更改方向。
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (orientationChangeAllowed) {
// how do I restart this activity?
} else {
// don't do anything
}
}
I want to make an activity that allows orientation changes on some condition, but not otherwise. More exactly I want to prevent restarting the activity when a background thread is busy.
I have put the configChanges
attribute on the activity manifest, and onConfigurationChanged
is called when the orientation changes. However I want to allow the app to change the orientation when allowed.
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (orientationChangeAllowed) {
// how do I restart this activity?
} else {
// don't do anything
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果允许则调用
setRequestedOrientation()
,如果不允许则不执行任何操作。提示:您可以使用
onRetainNonConfigurationInstance()
和getLastNonConfigurationInstance()
并返回(包含)AsyncThread
的对象。这样,Activity
将在用户需要时改变方向。但请注意:您不应该泄漏对Context
的引用(可能是对您的Activity
或Drawable
的引用,.. .)。if it's allowed call
setRequestedOrientation()
, when it's not allowed, do nothing.As a tip: You can use
onRetainNonConfigurationInstance()
andgetLastNonConfigurationInstance()
and return (an object containing) theAsyncThread
. This way theActivity
will change orientation when the user wants to. Take note though: You shouldn't leak a reference to aContext
(may it be a reference to yourActivity
or aDrawable
, ...).