3

action:sendBroadcast ACTION_LOCALE_CHANGED:

I registered the broadcast in the onCreate() for ACTION_LOCALE_CHANGED. Once broadcast receiver received and localeChanged() method will call. To get the changed locale using the below two APIs. A) getApplicationContext().getResources().getConfiguration() B) this.getResources().getConfiguration()

From the API (A) I can able to get the correct locale. But from the API (B) I could n't get able to get the updated/correct locale, its always giving the previous locale.

Below are the steps I tried:

  1. Open the Testapplication ->locale is in "en" (System locale also "en")
  2. Press home button, application went to background.
  3. I changed the system locale from "en" to "ja".
  4. All the systems has changed the locale to "ja"
  5. Again I opened the Testapplication (i.e Testapplication coming from background to Foreground)
  6. But the locale not changed, its still in the previous locale)

The below localeChanged() called whenever the ACTION_LOCALE_CHANGED broadcastreceiver received. That time application was in background.

In Android 6 and 7 this.getResources().getConfiguration() could able to bring the correct resources or locale. But android 8 and above we get this problem(locale not changed) when ACTION_LOCALE_CHANGED broadcastreceiver received.

In android emulator itself I can able to see this issue

onConfigurationChanged():

The above scenario working while using onCOnfigurationchanged().

Eventhough I can get the updated/correct locale through onConfigurationChanged(), but I want to know why its not getting the updated locale via ACTION_LOCALE_CHANGED broadcast and using this.getResources().getConfiguration() API.

Can anyone suggest any API differences are there android 7.1 and android 8.

Thanks in Advance!

Below are the source codes:

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG,"onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    IntentFilter testLocaleChangedFilter = new IntentFilter();
    testLocaleChangedFilter.addAction(Intent.ACTION_LOCALE_CHANGED);

    getApplicationContext().registerReceiver(new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
          localeChanged();
        }
    }, testLocaleChangedFilter);
}

LocaleChanged() method called when BroadcastReceiver() with Action: ACTION_LOCALE_CHANGED

private void localeChanged() {

   Configuration config = getApplicationContext().getResources().getConfiguration();
   Log.i(TAG, "localeChanged - getApplicationContext().getResources().getConfiguration():" + config);
  
   Configuration config2 = this.getResources().getConfiguration();
   Log.i(TAG, "localeChanged - this.getResources().getConfiguration():" + config2.locale);

   ((TextView)findViewById(R.id.text_language)).setText(R.string.language_text);
   
   Log.i(TAG,"getResources().getString(R.string.language_text):" + getResources().getString(R.string.language_text));}

@Override
public void onConfigurationChanged(android.content.res.Configuration newConfig) {
    Log.i(TAG, "onConfigurationChanged");
    super.onConfigurationChanged(newConfig);

    String locale = Locale.getDefault().toString();
    Log.i(TAG, "onConfigurationChanged(Locale):" + locale);

    Configuration config2 = this.getResources().getConfiguration();
    Log.i(TAG, "onConfigurationChanged-this.getResources().getConfiguration():" + config2.locale);}

0 Answers0