本文介绍了强制不同的区域设置仅适用于后堆栈中的顶级活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我遇到了关于应用本地化的一个小问题: 在这种情况下 - 我已给予用户始终 它适用于大多数部分(我去设置,选中框强制挪威语,按回和以前的活动以挪威语显示 - 同样的事情反过来),然而 - 语言变化似乎正确地更新(重新加载资源)为我的堆栈的第一个活动。 为了说明一个典型的场景:这里有明亮的心灵 相关源代码: 每个活动中包含的代码,用于设置显示语言: 在onCreate中: Globals.locale_default = Locale.getDefault()。getDisplayLanguage(); p> public void onStart(){ super.onStart(); forceNorwegian = settings.getBoolean(getString(R.string.pref_key_forceNorwegian).toString(),false); if(forceNorwegian){ languageCheck(no); } else { Globals.locale = null; languageCheck(Globals.locale_default); } } public void languageCheck(String lang){ Globals.locale = new Locale(lang); //检查当前系统区域设置,如果它不是默认值,则将其更改为挪威 Globals.checkLocale(this); if(Globals.language_changed){ //重新启动活动 Intent restart = getIntent(); finish(); Globals.language_changed = false; startActivity(restart); } } Globals.java: public class Globals { public static Locale locale = null; public static String locale_default = null; public static boolean language_changed = false; public static void checkLocale(Activity a){ if(locale == null) return; 配置config = a.getBaseContext()。getResources()。getConfiguration(); if(!config.locale.equals(locale)) {//更改为新的区域设置。一切都需要关闭或重新加载。 config.locale = locale; a.getBaseContext()。getResources()。updateConfiguration(config,null); language_changed = true; } } } 解决方案> 问题可能来自这样一个事实,即布尔Globals.language_changed是静态的,因此当从顶部activity调用languageCheck时,在从back活动中调用languageCheck之前,该布尔变为false。您可以进行一些检查以查看层次结构中较早的Activit是否已打开,如果是,则在布尔值设置为true的情况下(如果用户按后退按钮)。另一个选项是在选择新区域设置时立即重新加载所有打开的活动的一些逻辑。 - EDIT - $ b $进一步的考试,我不认为这是相当于你的问题,因为你也在更新布尔在onStart为每个活动(我错过了这部分,当我第一次读它)。也许区域设置更改时,其中一个活动更高的堆栈更改了它,但活动较低的堆栈只需要刷新。 - 编辑2 - 最简单的方法来检查是否是问题,将添加一些日志记录到Globals.checkLocale以查看该条件语句是否为真: if(!config.locale.equals(locale)) 那么一个可能的解决方案是在每个Activity中保存一个本地Locale实例,而不是一个全局实例,并与那个实例进行比较。例如,你可以在每个Activity的onCreate方法中获取一个Locale实例: myLocale = getBaseContext()getResources ().locale;然后,而不是调用Globals.checkLocal,只需执行以下条件语句(在每个Activity的languageCheck方法中): if(Globals.locale!= null&&!Globals.locale.equals(myLocale)) {配置config = getBaseContext()。getResources()。getConfiguration(); config.locale = Globals.locale; getBaseContext()。getResources()。updateConfiguration(config,null); Intent restart = getIntent(); finish(); startActivity(restart); } 重新启动时,Activity的onCreate方法会再次调用,到正确的值。这只是一个快速的解决方案,不一定是最好的一个..你可以扩展到这里移动一些代码到Globals中的一个方法,如果你想要的例子,或使用不同的位置比onCreate获取本地Locale实例每个活动。 I got a minor problem about app localization:Here's the case - I have given my users an option to always use the application in Norwegian, regardless of system language.It works just fine for the most part (I go to settings, check the box to force Norwegian, press "back" and the previous activity is displayed in Norwegian - same thing "the other way around"), however - the language change only seems to correctly update (reload resources) for the first activity in my "back stack".To illustrate a typical scenario:Are there any bright minds here with a suggestion to what I should do?Relevant source code:Code included in every activity to set the display language:In onCreate: Globals.locale_default = Locale.getDefault().getDisplayLanguage();public void onStart() { super.onStart(); forceNorwegian = settings.getBoolean(getString(R.string.pref_key_forceNorwegian).toString(), false); if (forceNorwegian) { languageCheck("no"); } else { Globals.locale = null; languageCheck(Globals.locale_default); }}public void languageCheck(String lang) { Globals.locale = new Locale( lang ); // Check the current system locale and change it to Norwegian if it's not already the default Globals.checkLocale( this ); if (Globals.language_changed) { // Restart activity Intent restart = getIntent(); finish(); Globals.language_changed = false; startActivity(restart); }}Globals.java:public class Globals {public static Locale locale = null; public static String locale_default = null; public static boolean language_changed = false; public static void checkLocale( Activity a ) { if( locale == null ) return; Configuration config = a.getBaseContext().getResources().getConfiguration(); if( !config.locale.equals( locale ) ) { // Change to the new locale. Everything will need to be closed or reloaded. config.locale = locale; a.getBaseContext().getResources().updateConfiguration( config, null ); language_changed = true; } }} 解决方案 The problem may be coming from the fact that the boolean Globals.language_changed is static, and therefore when languageCheck is called from the top activity, this boolean becomes false before languageCheck is called from the back activity. You might put in some checks to see if Activit(ies) earlier in the hierarchy are open, and if so keep the boolean set as true in case the user presses the Back button. Another option would be some logic that reloads all open Activities at once when the new locale is selected.-- EDIT --On further examination, I don't think this is quite your problem, since you are also updating the boolean in onStart for each activity (I missed this part when I read it the first time). Perhaps the locale changed when one of the Activities higher in the stack changed it, but the Activities lower in the stack just need to refresh. Does an orientation change on one of the lower Activities change it from English to Norwegian?-- EDIT 2 --The easiest way to check if that is the problem, would be to add some logging into Globals.checkLocale to see whether or not this conditional statement is true:if( !config.locale.equals( locale ) )If that turns out to be the problem, then one possible solution would be to save a local Locale instance in each Activity rather than a global one, and compare to that one. For example, you could grab a Locale instance in each Activity's onCreate method:myLocale = getBaseContext().getResources().getConfiguration().locale;Then, instead of calling Globals.checkLocal, just do the following conditional statement (in each Activity's languageCheck method):if( Globals.locale != null && !Globals.locale.equals( myLocale ) ){ Configuration config = getBaseContext().getResources().getConfiguration(); config.locale = Globals.locale; getBaseContext().getResources().updateConfiguration( config, null ); Intent restart = getIntent(); finish(); startActivity( restart );}Upon restarting, the Activity's onCreate method would get called again, which would update myLocale to the correct value. This is just a quick solution, not necessarily the best one.. you could expand on this to move some of that code into a method in Globals if you wanted for example, or use a different location than onCreate to get the local Locale instance for each Activity. 这篇关于强制不同的区域设置仅适用于后堆栈中的顶级活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-23 16:33