I'm using Compose in Fragment and here's my fragment code:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
binding.composeView.apply {
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
setContent {
MyTheme {
ChooseLanguageScreen()
}
}
}
}
when user choose a new language, I change app's locale and recreate activity using requireActivity().recreate()
.
after activity got recreated all resources including strings and drawables change and new resources are loaded except font family. I have included fonts for each locale in app resources:
to change fonts I have to restart the application. here's how I provide fonts in the theme:
val fonts = FontFamily(
Font(R.font.myFont, weight = FontWeight.Normal),
Font(R.font.myFont_bold, weight = FontWeight.Bold),
Font(R.font.myFont_medium, weight = FontWeight.Medium),
)
val myTypography = Typography(
body1 = TextStyle(
fontFamily = font,
fontWeight = FontWeight.Normal,
fontSize = 16.sp
),
subtitle2 = TextStyle(
fontFamily = font,
fontWeight = FontWeight.Normal
)
)
and here's the theme:
@Composable
fun MyTheme(content: @Composable () -> Unit) {
MaterialTheme(
colors = lightColorPalette,
typography = typography,
shapes = Shapes,
content
)
}
I guess I have to recompose the theme but I don't know how, any solutions?