Reputation: 962
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?
Upvotes: 4
Views: 2148
Reputation: 56
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.
Upvotes: 3