I tried to implement a language picker in my app (target api 33) according to this official guide android app-languages
But when I call AppCompatDelegate.setApplicationLocales(appLocale) method nothing happens. And after some debugging, I figured out that when the code reaches getLocaleManagerForApplication() method in AppCompatDelegate.java class static member sAppContext is null during all method.
@RequiresApi(33)
static Object getLocaleManagerForApplication() {
if (sLocaleManager != null) {
return sLocaleManager;
}
// Traversing through the active delegates to retrieve context for any one non null
// delegate.
// This context is used to create a localeManager which is saved as a static variable to
// reduce multiple object creation for different activities.
if (sAppContext == null) {
for (WeakReference<AppCompatDelegate> activeDelegate : sActivityDelegates) {
final AppCompatDelegate delegate = activeDelegate.get();
if (delegate != null) {
Context context = delegate.getContextForDelegate();
if (context != null) {
sAppContext = context;
break;
}
}
}
}
if (sAppContext != null) {
sLocaleManager = sAppContext.getSystemService(Context.LOCALE_SERVICE);
}
return sLocaleManager;
}
That's why method do nothing. I suppose that problem is that I inherit MainActivity from ComponentActivity, not AppCompatActivity. If I use AppCompatActivity I need to specify app compat theme in the manifest. But it is an entirely compose project. So how to fix this using ComponentActivity?
According to updated Google documentation and the answer given here, you have to use an AppCompat theme in your manifest.
<application
...
android:theme="@style/AppCompatTheme">
<activity .../>
</application>
Then, if your project is entirely Compose, use a Material theme inside your Activity's onCreate()
.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MaterialTheme {
...
}
}
}
}
I have an entirely Compose project with target API 33 too, and it works for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With