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?