偏好活动中的Android更新语言

时间:2011-10-17 15:15:43

标签: android configuration reload preferenceactivity

在我的Android应用程序中,我有一个偏好活动,让用户覆盖应用程序的语言。为实现这一点,我在每个活动的onResume()中调用此函数,然后重置内容视图:

    public static void checkOverrideSystemLanguage(Context context) {
    SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(context);

    // Check if system's language setting needs to be overridden
    boolean overrideSystemLanguage = prefs.getBoolean(context
            .getString(R.string.pref_key_chkbx_override_system_language),
            false);

    if (overrideSystemLanguage) {

        // Get language selection and possible languages
        String localeString = prefs.getString(
                context.getString(R.string.pref_key_list_languages), "");
        List<String> possibleLanguages = Arrays.asList(context
                .getResources().getStringArray(
                        R.array.pref_values_list_languages));

        if (possibleLanguages.contains(localeString)) {

            // Change language setting in configuration
            Locale locale = new Locale(localeString);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            context.getResources().updateConfiguration(config,
                    context.getResources().getDisplayMetrics());
        }
        // Use default language
        else {
            overrideSystemLanguage = false;
        }
    }

    // Use default language
    if (!overrideSystemLanguage) {
        context.getResources().updateConfiguration(
                Resources.getSystem().getConfiguration(),
                context.getResources().getDisplayMetrics());
    }
}

在每项活动中,它都能很好地运作。但是,在首选项活动中,当用户更改语言时,它不会立即更新,因为没有setContentView()方法。用户必须返回上一个活动并再次启动首选项活动,以查看反映的语言更改。

我在onPreferenceChange()侦听器中尝试了以下内容:

  1. 更新配置,删除和读取首选项:

    checkOverrideSystemLanguage(this);      
    // Remove all preferences and add them to update the language
    getPreferenceScreen().removeAll();
    addPreferencesFromResource(R.xml.preferences);
    
  2. 更新配置并调用onCreate(null)

  3. 完成并重新启动活动

    finish();
    startActivity(new Intent(this, PreferencesActivity.class));
    
  4. 感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我有类似的问题,我以这种方式解决了(正如你所说:完成并重新启动活动)。它有效!

    public class OptionsActivity extends PreferenceActivity implements YesNoDialogPreference.YesNoDialogListener, SharedPreferences.OnSharedPreferenceChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Options Setting
        final SharedPreferences prefs = getSharedPreferences(OptionsUtility.PREFERENCE_NAME, MODE_PRIVATE);
        String language = prefs.getString(OptionsUtility.PREFERENCE_LANGUAGE, OptionsUtility.DEFAULT_LANGUAGE);

        //Update locale
        OptionsUtility.updateLanguage(this, language);
        prefs.registerOnSharedPreferenceChangeListener(this);

        getPreferenceManager().setSharedPreferencesMode(MODE_PRIVATE);
        getPreferenceManager().setSharedPreferencesName(OptionsUtility.PREFERENCE_NAME);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);

        Preference eraseGameButton = getPreferenceManager().findPreference(OptionsUtility.PREFERENCE_ERASE_GAME);
        if (eraseGameButton != null) {
            YesNoDialogPreference yesNo = (YesNoDialogPreference)eraseGameButton;
            yesNo.setListener(this);
        }

        Preference configureKeyboardPref = getPreferenceManager().findPreference("keyconfig");
        if (configureKeyboardPref != null) {
            //KeyboardConfigDialogPreference config = (KeyboardConfigDialogPreference)configureKeyboardPref;
            //config.setPrefs(getSharedPreferences(MemodroidCoreActivity.PREFERENCE_NAME, MODE_PRIVATE));
            //config.setContext(this);
        }

    }


    public void onDialogClosed(boolean positiveResult) {
        if (positiveResult) {
            DatabaseScoreManager dbsManager = new DatabaseScoreManager(getApplicationContext());
            dbsManager.resetHighScores();
            Toast.makeText(this, R.string.toast_highScoresReset,Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String key) {
        restartActivity();
    }

    @Override
    protected void onStop() {
        super.onStop();
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }

    private void restartActivity() {
        Intent intent = getIntent();
        finish();
        startActivity(intent);
    }

    @Override
    public void onBackPressed() {
        finish();
    }
}