Android revolving screen's behind-the-scenes internationalization language failure solution, android Internationalization

Source: Internet
Author: User

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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.