Android 白天黑夜模式切换适配及引起的Activity销毁重启解决

2022年12月24日11:26:13

目录

一、白夜模式切换,Activity销毁重启解决:

二、 暗黑模式适配

 1. 添加依赖:

  2. 复制下面工具类方法直接使用即可:

3.资源适配:

 4. 适配回调:

5.切换白夜模式注意事项:


一、白夜模式切换,Activity销毁重启解决:

        当白夜模式切换时,activity会销毁重新加载,谷歌是希望重新加载Activity可以刷新页面UI,但我的App并没有适配深色模式,这样用户体验就很不好,解决办法就是:

在AndroidManifest.xml中给Activiyty添加或追加 android:configChanges="uiMode" 属性即可:

 configChanges 参数详解:

 

二、 暗黑模式适配

 1. 添加依赖:

api 'com.android.support:appcompat-v7:24.1.1' 或者更高版本
或使用androidx的依赖都可以

  2. 复制下面工具类方法直接使用即可:

/**
 * 暗黑模式适配工具类
 */
public class DarkModeUtils {

    public static final String KEY_MODE = "white_night_mode_sp";

    /**
     * 在 Application 的 onCreate() 方法中调用
     */
    public static void init(Application application) {
        int nightMode = getNightModel(application);
        AppCompatDelegate.setDefaultNightMode(nightMode);
    }

    /**
     * 应用夜间模式
     */
    public static void applyNightMode(Context context) {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        setNightModel(context, AppCompatDelegate.MODE_NIGHT_YES);
    }

    /**
     * 应用日间模式
     */
    public static void applyDayMode(Context context) {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        setNightModel(context, AppCompatDelegate.MODE_NIGHT_NO);
    }

    /**
     * 跟随系统主题时需要动态切换
     */
    public static void applySystemMode(Context context) {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
        setNightModel(context, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
    }

    /**
     * 判断App当前是否处于暗黑模式状态
     */
    public static boolean isDarkMode(Context context) {
        int nightMode = getNightModel(context);
        if (nightMode == AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) {
            int applicationUiMode = context.getResources().getConfiguration().uiMode;
            int systemMode = applicationUiMode & Configuration.UI_MODE_NIGHT_MASK;
            return systemMode == Configuration.UI_MODE_NIGHT_YES;
        } else {
            return nightMode == AppCompatDelegate.MODE_NIGHT_YES;
        }
    }

    private static int getNightModel(Context context) {
        SharedPreferences sp = context.getSharedPreferences(KEY_MODE, Context.MODE_PRIVATE);
        return sp.getInt(KEY_MODE, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
    }

    public static void setNightModel(Context context, int nightMode) {
        SharedPreferences sp = context.getSharedPreferences(KEY_MODE, Context.MODE_PRIVATE);
        sp.edit().putInt(KEY_MODE, nightMode).apply();
    }

}

3.资源适配:

       对图片和颜色创建暗黑模式的文件夹,切换暗黑模式时系统会自动使用该文件夹下的资源:

 4. 适配回调:

如果添加或追加了 android:configChanges="uiMode"属性, 在模式切换后会回调Activity的onConfigurationChanged(),建议在BaseActivity中重写该方法,并在里面做相应的适配.

可以调用上面工具类中的  DarkModeUtils.isDarkMode(this) 方法判断是否是暗黑模式.也可以调用工具类中的方法切换App的白夜模式,注意事项看下面:

5.切换白夜模式注意事项:

(1) 在 Application 的 onCreate() 方法中调用 DarkModeUtils.init(this) 初始化工具类;

(2) 调用 DarkModeUtils.applySystemMode(this) 方法自适应系统白夜模式切换;

(3) 如果使用的 androidx的依赖直接调用即可.

(4) 如果使用support依赖需要手动调用Activity的 recreate()方法重建页面或针对页面所有View做不同适配.

直接调用recreate()方法重建页面示例:

 针对页面每个View去设置白夜模式的示例:

(5) 具体原因看下面源码:

androidx版本:

/**
     * Sets the default night mode. This is the default value used for all components, but can
     * be overridden locally via {@link #setLocalNightMode(int)}.
     *
     * <p>This is the primary method to control the DayNight functionality, since it allows
     * the delegates to avoid unnecessary recreations when possible.</p>
     *
     * <p>If this method is called after any host components with attached
     * {@link AppCompatDelegate}s have been 'started', a {@code uiMode} configuration change
     * will occur in each. This may result in those components being recreated, depending
     * on their manifest configuration.</p>
     *
     * <p>Defaults to {@link #MODE_NIGHT_FOLLOW_SYSTEM}.</p>
     *
     * @see #setLocalNightMode(int)
     * @see #getDefaultNightMode()
     */
    public static void setDefaultNightMode(@NightMode int mode) {
        switch (mode) {
            case MODE_NIGHT_NO:
            case MODE_NIGHT_YES:
            case MODE_NIGHT_FOLLOW_SYSTEM:
            case MODE_NIGHT_AUTO_TIME:
            case MODE_NIGHT_AUTO_BATTERY:
                if (sDefaultNightMode != mode) {
                    sDefaultNightMode = mode;
                    applyDayNightToActiveDelegates();
                }
                break;
            default:
                Log.d(TAG, "setDefaultNightMode() called with an unknown mode");
                break;
        }
    }

support版本:

public static void setDefaultNightMode(int mode) {
        switch(mode) {
        case -1:
        case 0:
        case 1:
        case 2:
            sDefaultNightMode = mode;
            break;
        default:
            Log.d("AppCompatDelegate", "setDefaultNightMode() called with an unknown mode");
        }
    }

对比后可以发现androidx切换暗黑模式后,自己主动调用了applyDayNightToActiveDelegates()方法,使Activity重建。而support上没有,只是赋值。所以support版本上使用需要自己调用Activity的recreate()方法重建。

  • 作者:凌云志 !
  • 原文链接:https://blog.csdn.net/zhao8856234/article/details/126578124
    更新时间:2022年12月24日11:26:13 ,共 3900 字。