我一直想知道为什么我的 onCreate 方法运行两次,现在发现它与我在启动时设置应用程序的语言环境有关...我的问题是,它是否有必要运行两次或不是?
这是使 onCreate 运行两次的代码:
/*Sets the language of the application and also returns the integer value of selected language*/
protected Integer setLanguage() {
String lang = prefs.getString("language-key","0");
Integer language = Integer.parseInt(lang);
Configuration config = context.getResources().getConfiguration();
if (!decideLang(language).equals("") && !config.locale.getLanguage().equals(decideLang(language))) {
setLocale(decideLang(language));
}
return language;
}
/*Sets the locale*/
private void setLocale(String lang) {
((Activity) context).recreate();
Locale myLocale = new Locale(lang);
Resources res = context.getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
}
setLanguage 方法返回的整数稍后用于确定在稍后阶段要使用的 URL,但我已经意识到这对我的问题并不重要。
我的问题是,为什么 onCreate 需要因为这段代码运行两次?
BIG阳
相关分类