启动WebView后,Android Nougat 7.1重置语言环境 [英] Android Nougat 7.1 resets Locale after launching WebView

查看:133
本文介绍了启动WebView后,Android Nougat 7.1重置语言环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用Android N 7.1(API-25)出现了一个奇怪的现象,即启动WebView之后,系统会强制将Locale重置为设备locale。这会覆盖应用程序上使用的语言环境(用于本地化)。复制的简单方法是在应用程序上进行本地化。并启动一个WebView。然后,您将不再看到本地化的内容,直到再次重新启动应用程序为止。只有在Android-7.1(API-25)上会发生这种情况

We got a weird behavior with Android N 7.1 ( API-25 ) That after Launching WebView, system enforces reseting Locale to device locale. That overrides used locale (for localization) on application. Easy way to reproduce that is to get a localization on app. and launch a WebView. Then you won't see localized content any more until you relaunching app again. That happens only on Android-7.1 (API-25)

这是我切换可在所有API中使用的语言环境的方式:

Here is how I switch Locale which is working in all APIs:

 public void switchToCzLocale() {
        Locale mLocale = new Locale("cs","CZ");// it can be any other Locale
        Configuration config = getBaseContext().getResources()
                .getConfiguration();
        Locale.setDefault(mLocale);
        config.setLocale(mLocale);
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    }

我上传了一个样本来重现该问题,并提供以下更多详细信息:

I have uploaded a sample to reproduce that issue with more details on:

https://github.com/mabuthraa/ WebView-android7-issue

请知道此行为是否是错误的或者可能是更改语言环境的错误植入。

Please any idea if this behavior is a bug or probably bad implantation of changing locale.

以下是在Android组上发布票证的链接:问题218310:[开发人员预览]创建WebView会将语言环境重置为用户默认设置

Here is the link to issue ticket on Android group: Issue 218310: [developer preview] Creating a WebView resets Locale to user defaults

推荐答案

这是我的解决方法。

我们通过在初始化webView之后并在加载内容之前再次执行设置区域设置来解决该问题:

We resolved that issue by enforcing setting locale again after initializing webView and before loading content:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  MyApp.getApplication().switchToCzLocale();
}

例如在WebActivity中:

For example in WebActivity:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        mWebView = (WebView) findViewById(R.id.webview);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
          MyApp.getApplication().switchToCzLocale();
        }
        mWebView.loadData(getString(R.string.web_content), "text/html", "charset=UTF-8");
    }

MyApp:

import android.app.Application;
import android.content.res.Configuration;

import java.util.Locale;


public class MyApp extends Application {
    private static MyApp sApplication;

    @Override
    public void onCreate() {
        super.onCreate();
        switchToCzLocale();
        sApplication = this;
    }

    public static MyApp getApplication() {
        return sApplication;
    }

    public void switchToCzLocale() {
        Locale mLocale = new Locale("cs","CZ");
        Configuration config = getBaseContext().getResources()
                .getConfiguration();
        Locale.setDefault(mLocale);
        config.setLocale(mLocale);
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    }
}

我希望这会有所帮助,'。

I hope that may help,'.

我还是在寻找更好的解决方案。

这篇关于启动WebView后,Android Nougat 7.1重置语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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