Skip to content
This repository has been archived by the owner on Jun 22, 2022. It is now read-only.

ListPreference + v1.2.4 bug #41

Closed
dimkinware opened this issue Jan 6, 2020 · 1 comment
Closed

ListPreference + v1.2.4 bug #41

dimkinware opened this issue Jan 6, 2020 · 1 comment
Labels

Comments

@dimkinware
Copy link

Hello, I found a bug using v1.2.4 wih Preference dialog.
After changing language from selecting it from ListPreference dialog activity is recreated but language of the UI stays previous.
As I understand the reason is that in this case method Activity.getResources() is being called before Activity.attachBaseContext().
So LocalizationActivityDelegate.getResources() method change language in Resources object, then in subsequent attachBaseContext() method call next condition is not met:
if (!baseLocale.toString().equals(currentLocale.toString(), ignoreCase = true)) {

100% reproducing on Android 10 Pixel 2
v1.2.3 works fine.

To reproduce add next code in sample application:
SettingsActivity.java

package com.akexorcist.localizationapp.settingsactivity;

import android.os.Bundle;
import android.preference.PreferenceFragment;

import com.akexorcist.localizationactivity.ui.LocalizationActivity;
import com.akexorcist.localizationapp.R;

public class SettingsActivity extends LocalizationActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);
        getFragmentManager().beginTransaction().replace(R.id.main_content, new GeneralPreferenceFragment()).commit();
    }

    public static class GeneralPreferenceFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.settings);
            setHasOptionsMenu(false);
            findPreference("language").setOnPreferenceChangeListener((preference, newValue) -> {
                String lang = newValue.toString();
                ((LocalizationActivity)getActivity()).setLanguage(lang);
                return true;
            });
        }
    }
}

settings_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".settingsactivity.SettingsActivity">
    <FrameLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="?attr/actionBarSize" />
</FrameLayout>

settings.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory android:title="@string/hello_world">
        <ListPreference
            android:defaultValue="en"
            android:entries="@array/pref_language_list_titles"
            android:entryValues="@array/pref_language_list_values"
            android:key="language"
            android:negativeButtonText="@null"
            android:positiveButtonText="@null"
            android:title="language" />
    </PreferenceCategory>
</PreferenceScreen>

strings_settings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="pref_language_list_titles" translatable="false">
        <item>America</item>
        <item>China</item>
        <item>Italy</item>
        <item>Japan</item>
        <item>Korea</item>
        <item>Portugal</item>
        <item>Thai</item>
    </string-array>
    <string-array name="pref_language_list_values" translatable="false">
        <item>en</item>
        <item>zh</item>
        <item>it</item>
        <item>ja</item>
        <item>ko</item>
        <item>pt</item>
        <item>th</item>
    </string-array>
</resources>
@akexorcist akexorcist added the bug label Jan 15, 2020
@akexorcist
Copy link
Owner

Thank you for your feedback. I fixed it on version 1.2.5

Your code very helpful so I added it into example code.

akexorcist added a commit that referenced this issue Apr 15, 2020
akexorcist added a commit that referenced this issue Apr 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants