Jordi Saumell
02/06/2021, 7:41 PMupdateConfiguration(config, displayMetrics)
, which works fine but happens to be deprecated 😅.
All other ways that I know of rely on activity recreation (wrapping the context and using createConfigurationContext
, using attachBaseContext
, using applyOverrideConfiguration
).
Is there a non-deprecated way of doing this (without recreating the activity)?
(I think overwriting the language is considered not being the best practice, but it is quite common and many clients want it 🤷♂️)Albert Chang
02/07/2021, 3:13 AMoverride fun attachBaseContext(newBase: Context) {
val config = Configuration()
config.setLocale(<http://Locale.US|Locale.US>)
applyOverrideConfiguration(config)
super.attachBaseContext(newBase)
}
This works for me. The activity will not be re-created.Jordi Saumell
02/07/2021, 9:15 AMAlbert Chang
02/08/2021, 1:07 AMupdateConfiguration()
are the only ways to manually change configuration so I don't think you have any other choice. See here for how it is done in the AppCompat library.Albert Chang
02/08/2021, 1:09 AMupdateConfiguration()
is deprecated.
AppCompat needs to change the Activity’s resources configuration to enable “night mode”. The problems with WebView stemmed from using a now deprecated method:to achieve that. Unfortunately WebView doesn’t work very well with that method (hence the deprecation).Resources.updateConfiguration()
Jordi Saumell
02/08/2021, 8:32 AMJordi Saumell
02/08/2021, 8:33 AMAlbert Chang
02/08/2021, 9:10 AMAmbientConfiguration
(which is used internally in stringResource()
), the composer will automatically recompose the necessary parts when configuration changes thus recreation is not needed. If you really don't want the recreation and your app is compose-only, I think you can provide your own AmbientConfiguration
and AmbientContext
(I haven't tried, though).Jordi Saumell
02/08/2021, 10:57 PMval configuration = AmbientConfiguration.current
configuration.setLocale(Locale(myLocale))
val context = AmbientContext.current.createConfigurationContext(configuration)
Providers(AmbientContext provides context) {
Providers(AmbientConfiguration provides configuration) {
...
}
}
Setting only the configuration did not work. Setting only the context did work.
Is there any use on overriding the configuration (for changing the language)? Maybe for RTL?Albert Chang
02/09/2021, 12:13 AMAmbientLayoutDirection
. Btw, Providers
accepts varargs so you can simply write:
Providers(
AmbientContext provides context,
AmbientConfiguration provides configuration,
AmbientLayoutDirection provides TextUtils.getLayoutDirectionFromLocale(locale)
) {
...
}
Jordi Saumell
02/09/2021, 8:23 AMJordi Saumell
02/14/2021, 3:19 PMAmbientContext provides context
. It breaks the navigation backstack: when I press back on any screen it exits the app instead of going to the previous screen.
Setting configuration and layout direction does not cause this problem, but they don’t change the language either.
Is there any workaround that can be done by remembering some state? And is this a navigation bug? (I have a minimal project to reproduce the issue if needed)Albert Chang
02/15/2021, 1:29 AMJordi Saumell
02/15/2021, 9:48 PMAlbert Chang
02/16/2021, 12:12 AMOnBackPressedDispatcherOwner
) so NavHost cannot find an OnBackPressedDispatcher
. Since you are using navigation 1.0.0-alpha06 there seems no workaround. I suggest you upgrade to compose 1.0.0-alpha12 and navigation 1.0.0-alpha07 as there has been a huge API change and you can provide your own OnBackPressedDispatcherOwner
in navigation 1.0.0-alpha07 like this:
Providers(
LocalOnBackPressedDispatcherOwner.asProvidableCompositionLocal() provides this@MainActivity,
LocalConfiguration provides config,
LocalLayoutDirection provides layoutDirection,
LocalContext provides context
) {
val navController = rememberNavController()
...
}
Jordi Saumell
02/16/2021, 9:02 PMJordi Saumell
02/18/2021, 8:35 PMAlbert Chang
02/19/2021, 10:26 AMLaunchedEffect
. As for the problem, it looks like a bug to me. It seems that usage of a CompositionLocal
created by staticCompositionLocalOf
in the topBar
and bottomBar
of Scaffold
is not recomposed when the value changes (`CompositionLocal`s created by compositionLocalOf
are not affected, though). You might want to file a bug.Jordi Saumell
02/20/2021, 2:15 PM