onConfigurationChanged 有条件地重新启动 Activity

发布于 2024-08-25 08:18:59 字数 470 浏览 6 评论 0原文

我想做一个允许在某些条件下改变方向的活动,但在其他情况下不允许。更确切地说,我想防止在后台线程繁忙时重新启动活动。

我已将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的黑色迷你裙 2024-09-01 08:18:59

如果允许则调用setRequestedOrientation(),如果不允许则不执行任何操作。

提示:您可以使用 onRetainNonConfigurationInstance()getLastNonConfigurationInstance() 并返回(包含)AsyncThread 的对象。这样,Activity 将在用户需要时改变方向。但请注意:您不应该泄漏对 Context 的引用(可能是对您的 ActivityDrawable 的引用,.. .)。

if it's allowed call setRequestedOrientation(), when it's not allowed, do nothing.

As a tip: You can use onRetainNonConfigurationInstance() and getLastNonConfigurationInstance() and return (an object containing) the AsyncThread. This way the Activity will change orientation when the user wants to. Take note though: You shouldn't leak a reference to a Context (may it be a reference to your Activity or a Drawable, ...).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文