简体   繁体   English

如何在不丢失实例的情况下使用 onConfigChanges 刷新应用程序

[英]How to Refresh App with onConfigChanges without loosing instance


    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();
            }
        }

I want to change the app language without recreate the app.我想在不重新创建应用程序的情况下更改应用程序语言。 I mean I want to do it when user select any language from the setting then it Should change the Setting Activity language without using recreate();我的意思是当用户从设置中选择任何语言时我想这样做,然后它应该更改设置活动语言而不使用recreate(); , because when I use recreate(); ,因为当我使用recreate(); it blinks the App once.它使应用程序闪烁一次。

So I am not using recreate();所以我没有使用recreate(); . . Instead I wrote below code in Setting Activity in AndroidManifest.xml相反,我在AndroidManifest.xml中的设置活动中编写了以下代码

<activity
            android:name=".activity.SettingsActivity"
            android:label="@string/title_activity_settings"
            android:configChanges="orientation|keyboardHidden|screenSize|locale|layoutDirection"/>

As in below screenshot you can see that I have selected "Hindi" for the language but it is not updating the Activity to Hindi.在下面的屏幕截图中,您可以看到我为语言选择了“印地语”,但它没有将活动更新为印地语。 I mean "Select Country", "Select Your Language" and "Select Categories" should be display in Hindi instead of English.我的意思是“选择国家”、“选择您的语言”和“选择类别”应该用印地语而不是英语显示。 I have wrote String in both languages.我已经用两种语言编写了 String 。

Can Anybody know How to change it when Change the language ?任何人都可以在更改语言时知道如何更改它吗? OR Why onConfigChanges is not working for locale as it is working for Orientation.或 为什么 onConfigChanges 不适用于语言环境,因为它适用于 Orientation。

Thank you !谢谢!

在此处输入图片说明

In your MainActivity call sharedPrefrences on onStart callback insted of calling in onCreate在您的 MainActivity 调用 sharedPrefrences onStart 回调 insted 调用 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();
}

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());
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在我的一个标签中响应onConfigChanges? - How to respond for onConfigChanges in one of my tabs? 在不降低图标质量的情况下压缩应用程序中的图标 - compress icons in the app without loosing the quality of the icons 如何在不丢失图像内容的情况下调整图像大小 - How to resize image without loosing image content 如何在不失去分辨率的情况下放大和缩小位图? - How to zoom in and out to a bitmap without loosing resolution? 如何在不丢失我的ID和密码的情况下将我的应用程序从一页重定向到另一页 - how to redirect my app to from one page to another page without loosing my id and password in android 如何在不丢失格式的情况下更改ImageView - How to change an ImageView without loosing format 当我按下主页按钮时,如何使我的Android应用程序(具有蓝牙连接)工作而不会失去连接 - How to make my android app(has bluetooth connection) works without loosing connection when I press home button 加密用户数据而不会丢失在Android应用程序中登录的用户 - Encrypt the user data without loosing logged in user in android app 如何在不使用Android应用程序的刷新和重新创建的情况下重新启动应用程序? - How to restart App without using Refresh and ReCreate for Android app? 如何在不丢失“父”状态的情况下弹出 NavController 中的片段堆栈 - How to pop through fragment stack in NavController without loosing "parent" state
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM