Forcing A Different Locale Works Only For Top Activity In Back Stack
Solution 1:
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 ) )
{
Configurationconfig= getBaseContext().getResources().getConfiguration();
config.locale = Globals.locale;
getBaseContext().getResources().updateConfiguration( config, null );
Intentrestart= 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.
Solution 2:
I think its the line
Configurationconfig= a.getBaseContext().getResources().getConfiguration();
getBaseContext()
will only get you the reference to the Activity from inside another Acitivty. Hence, that's why it reflects the changes when you return back to the previous Activity but not the main application
Use getApplicationConext()
instead that reflects changes throughout the application. This will tie the changes to the entire lifecycle of your application, thus every Activity in your app
Post a Comment for "Forcing A Different Locale Works Only For Top Activity In Back Stack"