I'm having a very strange issue where if I change the language and recreate it will switch the day/night back to system default, and when I change the day/night it will change the language back to the system default. So, if I put these functions in the OnCreate event, it will be an endless loop. If I use the functions separately, they work as intended. But I can't change both.
This is how I change the language:
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1) public void SetLanguage()
{
Locale locale = new Locale("zh");
Locale.setDefault(locale);
Configuration configuration = new Configuration();
configuration.setLocale(locale);
getBaseContext().getResources().updateConfiguration(configuration, getBaseContext().getResources().getDisplayMetrics());
recreate();
}
This is how I change the day/night settings:
public void SetTheme(){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
//This method automatically recreates the activity, even if I don't have it specified
They are both placed OnCreate, I already tried placing them before and after super.onCreate(savedInstanceState) but the result is the same. Basically what's happening, is an endless loop where they are both affecting each other. Language changes the dark theme the back to light on recreate, and dark theme changes the language back to English when it recreates. So it happens forever. Is there a way to simultaneously change both and recreate only once? Thank you!
EDIT: Also, I have tried removing the recreate() after setting the language. But then if the theme is already dark in the system settings, then it won't recreate and the language change won't appear. That's not an ideal solution. I hope there's a way to change both and recreate() only once. Or if there's another more proper way to deal with this. Because many apps have this functionality. Thank you, again!