Change language on runtime in Android

可紊 提交于 2019-12-19 10:40:32

问题


I have to change the language on runtime in Android (yes, I know that this is not a good behaviour, but this is a requirement...).

So I have a basic class, from which every activity extends. This class has the following function:

public static void changeLanguage(Context context) {
    Resources res = context.getResources();
    /*
     * Change locale settings in the app.
     */
    DisplayMetrics dm = res.getDisplayMetrics();

    /*
     * Store and load data in this preferences
     */
    android.content.res.Configuration conf = res.getConfiguration();
    String[] localArray = res.getStringArray(R.array.language_short_array);
    if (localArray != null) {
        SharedPreferences settings = context.getSharedPreferences(
                MyService.APP_ID, MODE_PRIVATE);
        conf.locale = new Locale(localArray[settings.getInt(
                PREFERED_LANGUAGE_KEY, 0)]);
        res.updateConfiguration(conf, dm);
    }
}

I will call this method in onCreate:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    changeLanguage(this);
}

This is in the super-class. My Activities extends from it and call super.onCreate at first. After this call, they set their layout and initializes their settings...

I thought that my lines of code would make it. But I have the following problem: Sometimes, the activity changes the language and sometimes not!

If I set a debug breakpoint on it and after the programm pauses I press continue, everything works fine. So I think, in some cases, where my Application is "slow enough", the language will change correctly, whereas the language won't change if the application is too fast...

Is there any solution of my problem? How can I be sure, that my language will change correctly in any time?

Thanks a lot!

Edit: Here is an example for a class which extends from my super-class

public class MainMenuActivity extends BaseActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start);
    }
}

回答1:


changeLanguage(this); only needs to be called when the language has changed or when the App is loaded.. res.updateConfiguration(conf, dm); updates the global config and is specific to your app instance not your activity instance.

When you change the locale in an Activity you have to recreate that Activity to see your language change. This can be easily done by forcing an orientation change then forcing it back like this:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

If you hit back after a language change you will see the old language because onCreate is not called. You will have to detect in onResume that the language changed and force a recreate of the Activity.

-= EDIT =-

Using Screen Orientation to reload the Activity has proven to be a bit buggy on some devices. I am now using this to reload the current Activity:

public static void resetScreen(Activity activity) {
    if (activity != null) {
        if (Build.VERSION.SDK_INT >= 11) {
            activity.recreate();
        } else {
            Intent intent = activity.getIntent();
            activity.overridePendingTransition(0, 0);
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            activity.finish();

            activity.overridePendingTransition(0, 0);
            activity.startActivity(intent);
        }
    }
}



回答2:


I think you need to post an example of an Activity that extends your superclass. Calling changeLanguage in onCreate() seems suspicious to me, though. That's only going to run when the app is first initialized. To change the language once your app is loaded, you'd have to stop it and re-create it.



来源:https://stackoverflow.com/questions/16089750/change-language-on-runtime-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!