When my app is installed, on first run it asks user to choose language; But if user starts app on second time, language is set automatically (to previously selected one).
Problem is that, on first run when user selects language, app still is in English (no matter what language user selects), but if I rotate device, then language is changed to correct one (to selected one). Same problem occurs on apps second run; App starts in English, but after rotation language is changed to correct one.
Any suggestions? thank you in advance
here code from MainActivity from where I change language:
private void initApp() {
if (Settings.getLanguage().equals("")){
String[] items = { getResources().getString(R.string.lang_eng), getResources().getString(R.string.lang_geo)};
showDialogSingleChoise(items, getResources().getString(R.string.select_language_title), 0, false, false, this.languageSelectedListener, this.languageConfirmedListener, null);
} else {
System.out.println("initApp: " + Settings.getLanguage());
Settings.setLanguage(MainActivity.this.getResources(), Settings.getLanguage());
appVersionCheck();
}
}
private OnClickListener languageConfirmedListener = new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Settings.setLanguage(MainActivity.this.getResources(), MainActivity.this.language);
appVersionCheck();
}
};
and here is code fragment from settings:
public static void setLanguage(Resources res, String langString){
System.out.println("lang in pref: " + langString);
setStringProperty(PREFS_LANG ,langString);
MainApp.getInstance().setLocale(res);
}
Here is my Application class:
public class MainApp extends Application {
private static volatile MainApp instance;
public static MainApp getInstance() {
return MainApp.instance;
}
@Override
public void onCreate() {
MainApp.instance = this;
super.onCreate();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setLocale(this.getResources());
}
public void setLocale(Resources res) {
System.out.println("initApp: " + Settings.getLanguage() == "" ? Settings.LANG_EN : Settings.getLanguage());
Configuration conf = res.getConfiguration();
conf.locale = new Locale(Settings.getLanguage() == "" ? Settings.LANG_EN : Settings.getLanguage());
res.updateConfiguration(conf, res.getDisplayMetrics());
}
}
and lastly, this is manifest (do I need android:configChanges="locale" at all?):
<application
android:name="...MainApp"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="....MainActivity"
android:configChanges="locale"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>