android 错误或配置对象的误用?

标签 android configuration locale

我们在 Application 对象的 onCreate 方法中有一些代码可以更改 ApplicationContext 配置中的默认本地。

它看起来像:

Locale locale = new Locale(sSavedLocale);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getResources().updateConfiguration(config, getResources().getDisplayMetrics());

然后我们开始我们的主要 Activity ,然后从那里开始我们的其他 Activity 和任务。 只要我不引起屏幕旋转等配置更改,一切都可以。 如果我旋转屏幕,Locale 将返回到设备默认值,即使应用程序对象保持不变。

深入研究 ActivityThread 和其他内部类的 android 源代码,我可以看到 ApplicationContext 本身和 mainThread 中的上下文在配置更改事件时更改回默认值。

这对我来说似乎是一个错误,因为我在应用程序启动时在应用程序级别设置了配置,我可能想保持更改,除非发生本地配置更改,即使这样......

我没有检查我确实想应用的开放错误。 有人认为我错了,这是可以的行为吗?

最佳答案

您可以将 onConfigurationChanged 添加到您的 Application 类。

    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
        if (newConfig.locale != null)
        {
            Locale locale = new Locale(sSavedLocale);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getResources().updateConfiguration(config, getResources().getDisplayMetrics());
        }
    }

关于android 错误或配置对象的误用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14009870/

相关文章:

c++ - printf 输出不受全局语言环境影响?

android - 如何在没有 Play 商店的情况下自动更新 Android 应用程序?喜欢 Facebook 应用或任何竞赛应用

android - Image Url 存储在相机和图库 Intent 的 Realm 中,但在 ListView 中看不到

linux - 我可以使用 puppet 停止 Linux 服务器上的服务吗?

java - 在 Java 中更改打印流的语言环境

json - jq、区域设置和 strptime : how to use properly

java - 使用 Gradle 仅编译时依赖项和 ProGuard

android - Activity 中的 OnOptionsItemSelected 在 fragment 中的 onOptionsItemSelected 之前被调用。其他方式可能吗?

visual-studio - 如何在 Visual Studio 中获得 Emacs 样式的键绑定(bind)?

configuration - 从 WIX 设置更新 app.config?