Don't work SetContent{} function when i call recreate function for change my app theme
Hi there
i try to change theme in my app that developed with jetpack compose(material v1.2.0-alpha12) and android version 13(API 30)
but don't work reCreate function for activity after submitted configuration
i try to put Log.i in my code and debug it, that all i know is don't work setContent{} when call again after reCreate lifecycle
can anybody help me?
After tinkering around for quite some time, I've managed to update the
Localeand recreate the activity to change the app language, also working on Android 13 (API 33) by using the new localization approach.I've included an example guide of how to achieve it, including how to change the theme in Compose.
It's important to note that I've used Preferences DataStore as a solution for storing preferences instead of Shared Preferences since it has many advantages (e.g. real-time observation without a listener), in combination with dependency injection using DaggerHilt.
I highly recommend using a single activity with a navigation for different screens since it's easier to maintain and the app doesn't get filled with activities. Here is a detailed guide of how to implement such navigation.
The structure of the example guide:
Feel free to reach out to me if you need further assistance.
Let's start by creating a list containing the app languages:
It's important to note that the list must contain ONLY languages that are already a translated variant of the
strings.xml.I recommend using the Translations Editor to make a translated variant.
Then, creating a function that can be called within an
Activity, containing the necessary logic to set the language. I highly recommend to put this function inside a separate Kotlin file, so it's not coupled to a singleActivityand then calling it within theonCreatefunction of anActivityin a lifecycle manner (As shown in the final example):Changing the theme in Compose can be done by using the following approach:
ThemeModeenum class, containing the necessary theme modes, like:SYSTEM,LIGHT, andDARK.LocalThemeModeCompositionLocal key for providing and consuming theThemeModedown the composition.isInDarkTheme()composable function to check if the currently applied theme is dark.ThemeModethat can be observed in real time. (as shown later on):darkTheme: Booleanparameter withthemeMode: ThemeMode, and provide aLocalThemeModekey usingCompositionLocalProvider, and declare adarkThemeproperty:Creating the Preferences DataStore:
AppPreferencescontaining the logic for saving and reading the preferences:Setting up the dependency injection:
@HiltAndroidAppwhich is also included as aandroid:nameattribute in the app manifestapplicationtag:AppPreferencesModule, which provides a single instance of theAppPreferencesfor injection:A ViewModel containing the states and callbacks:
The ViewModel has an injected instance of the
AppPreferenceswhich are transformed into states for a real-time observation by the UI:The callbacks to change the preferences can be implemented in another ViewModel (e.g. of a Settings screen, which also has the
AppPreferencesinstance injected).Finally, the Activity implementation:
The activity is annotated with
@AndroidEntryPointwhich marks an Android component class to be setup for injection.The
setAppLanguage()function is executed inside the collected in a lifecycle mannerlanguageState.The
themeModeStateis collected in a lifecycle manner as well, and it's passed as an argument to the already adapted application theme composable.And there you have it, an example way of updating the
Localeand changing the theme in Compose.Feel free to reach out to me if you need further assistance.
Happy Coding!