Reputation: 3789
I have to change the language on runtime in Android (yes, I know that this is not a good behaviour, but this is a requirement...).
So I have a basic class, from which every activity extends. This class has the following function:
public static void changeLanguage(Context context) {
Resources res = context.getResources();
/*
* Change locale settings in the app.
*/
DisplayMetrics dm = res.getDisplayMetrics();
/*
* Store and load data in this preferences
*/
android.content.res.Configuration conf = res.getConfiguration();
String[] localArray = res.getStringArray(R.array.language_short_array);
if (localArray != null) {
SharedPreferences settings = context.getSharedPreferences(
MyService.APP_ID, MODE_PRIVATE);
conf.locale = new Locale(localArray[settings.getInt(
PREFERED_LANGUAGE_KEY, 0)]);
res.updateConfiguration(conf, dm);
}
}
I will call this method in onCreate:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
changeLanguage(this);
}
This is in the super-class. My Activities extends from it and call super.onCreate at first. After this call, they set their layout and initializes their settings...
I thought that my lines of code would make it. But I have the following problem: Sometimes, the activity changes the language and sometimes not!
If I set a debug breakpoint on it and after the programm pauses I press continue, everything works fine. So I think, in some cases, where my Application is "slow enough", the language will change correctly, whereas the language won't change if the application is too fast...
Is there any solution of my problem? How can I be sure, that my language will change correctly in any time?
Thanks a lot!
Edit: Here is an example for a class which extends from my super-class
public class MainMenuActivity extends BaseActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
}
}
Upvotes: 2
Views: 2438
Reputation: 7329
changeLanguage(this);
only needs to be called when the language has changed or when the App is loaded.. res.updateConfiguration(conf, dm);
updates the global config and is specific to your app instance not your activity instance.
When you change the locale
in an Activity
you have to recreate that Activity
to see your language change. This can be easily done by forcing an orientation change then forcing it back like this:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
If you hit back after a language change you will see the old language because onCreate
is not called. You will have to detect in onResume
that the language changed and force a recreate of the Activity
.
-= EDIT =-
Using Screen Orientation to reload the Activity
has proven to be a bit buggy on some devices. I am now using this to reload the current Activity
:
public static void resetScreen(Activity activity) {
if (activity != null) {
if (Build.VERSION.SDK_INT >= 11) {
activity.recreate();
} else {
Intent intent = activity.getIntent();
activity.overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
activity.finish();
activity.overridePendingTransition(0, 0);
activity.startActivity(intent);
}
}
}
Upvotes: 3
Reputation: 8641
I think you need to post an example of an Activity that extends your superclass. Calling changeLanguage in onCreate() seems suspicious to me, though. That's only going to run when the app is first initialized. To change the language once your app is loaded, you'd have to stop it and re-create it.
Upvotes: 0