I have implemented two language in my app \"English\" and \"French\" and both of them are working fine individually. When I am trying to check the language change with devic
You have two different options.
android:configChanges Lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.
Note: Using this attribute should be avoided and used only as a last-resort. Please read Handling Runtime Changes for more information about how to properly handle a restart due to a configuration change.
Any or all of the following strings are valid values for this attribute. Multiple values are separated by '|' — for example, "locale|navigation|orientation".
Check this,
String language = preferences.getString("language", null);
and this is my onclick listener of button
llChangeLanguage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (language == null) {
LocaleHelper.setLocale(BaseActivity.this, "de");
Intent intent = new Intent(BaseActivity.this, HomeActivity.class);
storeLanguageInPref("en");
startActivity(intent);
finish();
} else if ("kn".contentEquals(language)) {
LocaleHelper.setLocale(BaseActivity.this, "de");
Intent intent = new Intent(BaseActivity.this, HomeActivity.class);
startActivity(intent);
storeLanguageInPref("en");
finish();
} else {
LocaleHelper.setLocale(BaseActivity.this, "kn");
Intent intent = new Intent(BaseActivity.this, HomeActivity.class);
storeLanguageInPref("kn");
startActivity(intent);
finish();
}
}
});
and here i am storing the selected language to preference
private void storeLanguageInPref(String language) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(BaseActivity.this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("language", language);
editor.apply();
}
LocaleHelper class
public class LocaleHelper {
public static Context onAttach(Context context) {
String locale = getPersistedLocale(context);
return setLocale(context, locale);
}
public static String getPersistedLocale(Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(SettingsFragment.KEY_PREF_LANGUAGE, "");
}
/**
* Set the app's locale to the one specified by the given String.
*
* @param context
* @param localeSpec a locale specification as used for Android resources (NOTE: does not
* support country and variant codes so far); the special string "system" sets
* the locale to the locale specified in system settings
* @return
*/
public static Context setLocale(Context context, String localeSpec) {
Locale locale;
if (localeSpec.equals("system")) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = Resources.getSystem().getConfiguration().getLocales().get(0);
} else {
//noinspection deprecation
locale = Resources.getSystem().getConfiguration().locale;
}
} else {
locale = new Locale(localeSpec);
}
Locale.setDefault(locale);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return updateResources(context, locale);
} else {
return updateResourcesLegacy(context, locale);
}
}
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, Locale locale) {
Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
configuration.setLayoutDirection(locale);
return context.createConfigurationContext(configuration);
}
@SuppressWarnings("deprecation")
private static Context updateResourcesLegacy(Context context, Locale locale) {
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLayoutDirection(locale);
}
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return context;
}
}