如何使用onConfigChanges刷新应用程序而不会丢失实例

时间:2016-08-23 14:52:27

标签: android android-studio android-activity locale instancestate



protected void onResume() {
        super.onResume();


        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        Configuration config = getBaseContext().getResources().getConfiguration();

        String lang = settings.getString("lang_list", "");
        // Log.d("Lang",lang);
        if (!"".equals(lang) && !config.locale.getLanguage().equals(lang)) {
            recreate();
            Locale locale = new Locale(lang);
            Locale.setDefault(locale);
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
        }

        if (AppController.getInstance().isPreferenceChanged()) {
            setupViewPager(viewPager);
            adapter.notifyDataSetChanged();
        }
    }




我想在不重新创建应用的情况下更改应用语言。我的意思是当用户从设置中选择任何语言时我想这样做它应该在不使用recreate();的情况下更改设置活动语言,因为当我使用recreate();时它会使应用程序闪烁一次。

所以我没有使用recreate();。相反,我在AndroidManifest.xml

中的设置活动中编写了以下代码
<activity
            android:name=".activity.SettingsActivity"
            android:label="@string/title_activity_settings"
            android:configChanges="orientation|keyboardHidden|screenSize|locale|layoutDirection"/>

如下面的屏幕截图所示,您可以看到我选择了&#34; Hindi&#34;对于该语言,但它没有将活动更新为印地语。我的意思是&#34;选择国家&#34;,&#34;选择你的语言&#34;和&#34;选择类别&#34;应该用印地语而不是英语显示。我用两种语言写过String。

任何人都可以知道更改语言时如何更改它? 要么 为什么onConfigChanges不适用于语言环境,因为它适用于Orientation。

谢谢!

enter image description here

2 个答案:

答案 0 :(得分:0)

In your MainActivity call sharedPrefrences on onStart callback insted of calling in onCreate

Example:-

protected void onStart() {
    SharedPreferences sharedPreferences = this.getSharedPreferences("selectedLanguage", Context.MODE_PRIVATE);
    String pine = sharedPreferences.getString("language", DEFAULT);
    String languageToLoad = pine;
    Locale locale = new Locale(languageToLoad);//Set Selected Locale
    Locale.setDefault(locale);//set new locale as default
    Configuration config = new Configuration();//get Configuration
    config.locale = locale;//set config locale as selected locale
    this.getResources().updateConfiguration(config, this.getResources().getDisplayMetrics());
    invalidateOptionsMenu();  //This changes language in OptionsMenu too
    setTitle(R.string.app_name);  //calling app name according to language selected by user
    super.onStart();
}

答案 1 :(得分:0)

You can use a singleton class that will handle the language change without restarting the activity,

public class MyApplication extends Application
{
    private Locale locale = null;

    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
        if (locale != null)
        {
            newConfig.locale = locale;
            Locale.setDefault(locale);
            getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
        }
    }

    @Override
    public void onCreate()
    {
        super.onCreate();

        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);

        Configuration config = getBaseContext().getResources().getConfiguration();

        String lang = settings.getString(getString(R.string.pref_locale), "");
        if (! "".equals(lang) && ! config.locale.getLanguage().equals(lang))
        {
            locale = new Locale(lang);
            Locale.setDefault(locale);
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
        }
    }
}