Completely stop or restart all actions in the Android application to reflect the new language choice

I am working on an Android application that should be bilingual (English / Spanish). I allow the user to select the desired language from Preferences and perform the following locale changes in the main activity of the application (subclass TabActivity) based on the selected language:

private void setApplicationLanguage(String languageCode) { // Set the locale to the specified language code. Resources res = getResources(); DisplayMetrics dm = res.getDisplayMetrics(); android.content.res.Configuration conf = res.getConfiguration(); conf.locale = new Locale(languageCode.toLowerCase()); res.updateConfiguration(conf, dm); } 

In the main action, four tabs are placed, each of which has its own activity (sometimes an ActivityGroup). What I would like to have is when the language changes, all open actions just restart, which forces them to reload lines in the corresponding language. It would also be acceptable (but less desirable) to completely destroy all open actions, requiring the user to start the application again manually, and therefore all lines will be reloaded in the corresponding language.

I tried calling finish() from the main action, which is why this activity stops. However, when I restart the application, the child actions "hosted" by the main action (one for each tab) are still displayed in the previous language. I guess this is because children's activities were not recursively "completed" for me.

I also tried to include / exclude the locale attribute from AndroidManifest.xml in the main action, as well as other actions, but I never see the activity restart when the local changes. Again, how will a โ€œrestartโ€ manifest itself? Calling onCreate() or just onResume() or even something else? If this approach automatically restarted the action by calling onCreate() when changing the locale, then this sounds like a concrete guide on how to do this, would be the best way in my case.

The solution should run at the Android 7 API level, so creating a new Intent with the FLAG_ACTIVITY_CLEAR_TASK flag is apparently not available.

+4
source share
2 answers

You can use BroadcastReceiver as an inner class for each Activity used for the contents of the tab.

Register the receiver in onResume and reinstall it in onPause .

Ask the main Activity to send a sticky translation when the locale changes and use the registerReceiver result in onResume to retrieve the Intent to see if the locale has changed. This should also work for any โ€œvisibleโ€ Activity so that it can dynamically update data.

+1
source

I implemented LocalBroadcastManager to tell stakeholders about a language change. The problem was that I could not update everything that I wanted, for example. d. tab shortcuts. What turned out best was the following in the โ€œmainโ€ (Tab) activity:

  Intent intent = new Intent(mainActivity.getApplicationContext(), MainTabActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); mainActivity.startActivity(intent); 
0
source

Source: https://habr.com/ru/post/1414031/


All Articles