1

I want to make an opportunity to change language in my single activity app. When app's Locale was changed the onConfigurationChanged() method must be called by system and I think I can use it to recreate Activity but as I see in logs onConfigurationChanged() was not called. Here is a part from my Manifest

         <activity
            android:name=".presentation.activities.main.MainActivity"
            android:exported="true"
            android:launchMode="singleTask"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustResize"
            android:configChanges="layoutDirection|locale|orientation|screenSize"
>
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <!--     filter for DeepLink handling       -->
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data
                    android:host="@string/app_host_main"
                    android:scheme="@string/app_scheme" />
            </intent-filter>
        </activity>

My app supports writing from right to left (android:supportsRtl="true")

There is a part of my MainActivity code, the default local is Russian, i change it in the onCreate method to English

     override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        installSplashScreen()

        setContentView(viewBinding.root)

        // Setting up start destination
        defineStartDestination()

        setupBottomNavigationView()

        val config = Configuration(this.resources.configuration)
        val locale = Locale("en")
        Locale.setDefault(locale)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            config.setLocale(locale)
            Log.d("Kty", "am1")
        } else {
            config.locale = locale
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            this.createConfigurationContext(config)
            this.resources.updateConfiguration(config, this.resources.displayMetrics)
            Log.d("Kty", "am2")
        }
        config.setLocale(locale)
        config.setLayoutDirection(locale)
        this.createConfigurationContext(config)
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        Log.d("Kty", "Conf changed")
        super.onConfigurationChanged(newConfig)
        Log.d("Kty", "Conf changed")
    }

I see in logs "am1", "am2" but not "Conf changed". App show texts in English so locale was changed (activity is the host for fragments and they are in English) What i do wrong? Why onConfigurationChanged() is not called?

MihaelKrau
  • 31
  • 1

2 Answers2

0

Try setting

android:configChanges="layoutDirection|locale"

in the manifest. Or try looking at a similar question.

Adam
  • 186
  • 1
  • 4
  • 20
0

If you just want set the locale programmatically check this stack: Set Locale programmatically

Otherwise , if you just want to know when the language is changed, you can use a BroadCastReceiver, inside of a Broadcast:

 @Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_LOCALE_CHANGED)) {

        Locale currentLocale = Locale.getDefault();
        // Notify the locale change where you want.
        onLocaleChanged(currentLocale);

    }

}
Pablo
  • 48
  • 5