Android Nougat 7.1 resets Locale after launching WebView

后端 未结 1 836
庸人自扰
庸人自扰 2020-12-29 08:20

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 localiz

相关标签:
1条回答
  • 2020-12-29 08:57

    Here is my workaround solution.

    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();
    }
    

    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,'.

    Still Im looking for a better solution.

    0 讨论(0)
提交回复
热议问题