When the Android system switches the language, the APP cannot update the resource file. androidapp

Source: Internet
Author: User

When the Android system switches the language, the APP cannot update the resource file. androidapp

Your APP supports multi-language Environments. If it is running in the background, you can change the system language and open it directly from the background after the system language is changed, at this time, the user will find that your interface language is not consistent with the system language. Why? Mainly because the interface resources are not reloaded

If you set language switching in the APP, you can directly process the switching activity and restart the activity once, however, if your APP is similar to me and does not have the function of switching language, but follows the system, you cannot process it in all the activities! So I will restart the app in the application to solve the resource update problem.

We all know that Application is the first entry to Android, so once the system switches the language, it will trigger its onConfigurationChanged () method, and then we will process it directly in this method after restarting, the finishActivity (); method is to close all the activities, so you need to execute MyApp in BaseActivity. getInstance (). addActivity (this );

Public class MyApp extends Application {private static MyApp instance = null; private List <Activity> activities = new ArrayList <Activity> (); public void addActivity (Activity activity) {activities. add (activity);} public void finishActivity () {for (Activity activity: activities) {activity. finish () ;}} public static MyApp getInstance () {if (instance = null) {instance = new MyApp () ;}return instance ;}@ Override public void onCreate () {// TODO Auto-generated method stub super. onCreate (); instance = this ;}@ Override public void onConfigurationChanged (Configuration newConfig) {// TODO Auto-generated method stub super. onConfigurationChanged (newConfig); // configuration change triggers this method finishActivity (); // The language switch exits // The following method is directly used at the beginning, during the test, it is found that if the app starts multiple activities, this method will cause the app to restart several times, and finally the activity is closed in batches to solve the issue. // System. exit (0); // android. OS. process. killProcess (android. OS. process. myPid ());}}

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.