when using

getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK

to check what mode the app is currently in,

int currentNightMode = getResources().getConfiguration().uiMode
        & Configuration.UI_MODE_NIGHT_MASK
switch (currentNightMode) {
    case Configuration.UI_MODE_NIGHT_NO:
        // Night mode is not active, we're in day time
    case Configuration.UI_MODE_NIGHT_YES:
        // Night mode is active, we're at night!
    case Configuration.UI_MODE_NIGHT_UNDEFINED:
        // We don't know what mode we're in, assume notnight
}

if called this with AppCompatDelegate.MODE_NIGHT_YES earlier

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);

is the return of currentNightMode to be Configuration.UI_MODE_NIGHT_YES?

what it would return when the AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM was set before

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);

and the device has changed from light to dark them (or from dark to light)?

2

There are 2 answers

0
seied mostafa Eftekhary On
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        themeSystem.setVisibility(View.VISIBLE);
    } else {
        themeSystem.setVisibility(View.GONE);
    }
    switch (sharePref.getTheme()) {
        case THEME_LIGHT:
            themeLight.setChecked(true);
            break;
        case THEME_DARK:
            themeDark.setChecked(true);
            break;
        case THEME_SYSTEM:
            themeSystem.setChecked(true);
            break;

        default:
            switch (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) {
                case Configuration.UI_MODE_NIGHT_NO:
                    themeLight.setChecked(true);
                    break;
                case Configuration.UI_MODE_NIGHT_YES:
                    themeDark.setChecked(true);
                    break;
                case Configuration.UI_MODE_NIGHT_UNDEFINED:
                    themeLight.setChecked(true);
                    break;
            }
    }


    themeGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            switch (checkedId) {
                case R.id.themeLight:
                    setTheme(AppCompatDelegate.MODE_NIGHT_NO, THEME_LIGHT);
                    break;
                case R.id.themeDark:
                    setTheme(AppCompatDelegate.MODE_NIGHT_YES, THEME_DARK);
                    break;
                case R.id.themeSystem:
                    setTheme(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM, THEME_SYSTEM);
                    break;
            }
        }
    });
0
lannyf On
context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES

tells current what mode the app will be in.

when

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)

if change the system theme in settings (in Android Q), the configuration.uiMode will reflect the change.

same with the

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
or
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)

note: the configuration.uiMode change will trigger a config change and may cause recreate the activity.