2

I have an app that sets the locale based on user input. This is working fine for all views except one fragment.

During:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (rootView == null) {
        rootView = inflater.inflate(R.layout.fragment_booking, container, false);

It changes the locale... If I debug, before the inflate-line the getResources().getConfiguration().locale is the locale the user has selected. But after this line, the same value has changed to the Phone Device's locale... This is not happening in other fragments even though it's written exactly the same way.

This is the code that sets the locale:

public static void setApplicationLanguage(Context context) {
    try {
        String languageCode = StorageManager.getInstance().getStringValue(MarketRegions.SELECTED_LANGUAGE, DEFAULT_LANGUAGE);
        Locale locale = new Locale(StringUtils.substring(languageCode, 0, 2), StringUtils.substring(languageCode, 3, 5));

        Resources resources = context.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        Configuration conf = resources.getConfiguration();
        conf.locale = locale;

        resources.updateConfiguration(conf, dm);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Does anyone know why this is happening? Is it a bug in Android?

I can give more info if needed. Thanks! :)

Otziii
  • 2,304
  • 1
  • 25
  • 34

0 Answers0