Android revolving screen's behind-the-scenes internationalization language failure solution, android Internationalization
This article has been synchronized to my blog: liyuyu.cn
I recently used international languages (English + Chinese) in the project, but I found a problem during use. When the screen is rotated, the APP language (Chinese) automatically converted to the system language (English). The android: configChanges = "orientation | screenSize" attribute of the Activity is invalid, so I asked Stackoverflow for help, you know, and finally solved the problem, so I sorted this article for reference.
1. Create the FunctionApplication class to inherit the Application and override onConfigurationChanged. The Code is as follows:
public class FunctionApplication extends Application{ @Override public void onConfigurationChanged(Configuration newConfig) { // TODO Auto-generated method stub super.onConfigurationChanged(newConfig); toChinese(); } public void toChinese() { String languageToLoad = "zh"; Locale locale = new Locale(languageToLoad); Locale.setDefault(locale); Configuration config = getResources().getConfiguration(); DisplayMetrics metrics = getResources().getDisplayMetrics(); config.locale = Locale.SIMPLIFIED_CHINESE; getResources().updateConfiguration(config, metrics); }}
2. Modify the AndroidManifest. xml file and specify the application node as our custom FunctionApplication.
<application android:name="com.xxx.xxxx.FunctionApplication" android:allowBackup="true" android:configChanges="orientation|screenSize|locale" android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/AppTheme" >
At this point, the problem of language failure behind the scenes of the revolving screen can be solved. After reading the relevant information, we found that when the onConfigurationChanged (Configuration newConfig) method is triggered during screen rotation, newConfig takes the system, which is why the language is switched to the system language, so here we can set locale again.