I faced a problem at the time of day/night mode change from fragment that without recreate the parent activity the mode not change but I want to change the mode from fragment without move from the current fragment.
public void setNightMode(Context target, boolean state){
//UiModeManager uiManager = (UiModeManager) target.getSystemService(Context.UI_MODE_SERVICE);
UiModeManager uiManager = (UiModeManager) getActivity().getSystemService(Context.UI_MODE_SERVICE);
if (state) {
//setTheme(R.style.DarkTheme);
if (android.os.Build.VERSION.SDK_INT > Build.VERSION_CODES.M){
//setting up Night Mode...
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
uiManager.setNightMode(UiModeManager.MODE_NIGHT_YES);
}
//uiManager.setNightMode(UiModeManager.MODE_NIGHT_YES);
night_mode = false;
//homeBinding.toolbar.setHeaderColour(HomeActivity.this, false);
} else {
if (android.os.Build.VERSION.SDK_INT > Build.VERSION_CODES.M){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
} else {
uiManager.setNightMode(UiModeManager.MODE_NIGHT_NO);
}
night_mode = true;
}
}
for change to night mode :
and change to the light mode:
also for use this feature you must implement the androidX appcompat library