fix: BackgroundUtils初始化时自动保存配置至SP

- initFromResource 和 initFromColor 增加自动调用 saveToPreferences()
- 优化 initFromPreferences 默认逻辑,避免重复调用保存方法
This commit is contained in:
2026-05-02 10:32:58 +08:00
parent aea9f1d745
commit 03ae37dc91

View File

@@ -50,6 +50,7 @@ public class BackgroundUtils {
utils.drawableType = DrawableType.RESOURCE_ID; utils.drawableType = DrawableType.RESOURCE_ID;
utils.resId = resId; utils.resId = resId;
utils.drawable = ContextCompat.getDrawable(utils.context, resId); utils.drawable = ContextCompat.getDrawable(utils.context, resId);
utils.saveToPreferences();
return utils; return utils;
} }
} }
@@ -61,6 +62,7 @@ public class BackgroundUtils {
utils.drawableType = DrawableType.COLOR; utils.drawableType = DrawableType.COLOR;
utils.color = color; utils.color = color;
utils.drawable = new ColorDrawable(color); utils.drawable = new ColorDrawable(color);
utils.saveToPreferences();
return utils; return utils;
} }
} }
@@ -79,9 +81,8 @@ public class BackgroundUtils {
int color = prefs.getInt(KEY_COLOR, Color.BLACK); int color = prefs.getInt(KEY_COLOR, Color.BLACK);
return initFromColor(appContext, color); return initFromColor(appContext, color);
} }
BackgroundUtils utils = initFromColor(appContext, 0xFF00FF00); // 默认情况initFromColor 内部已经调用了 saveToPreferences()
utils.saveToPreferences(); return initFromColor(appContext, 0xFF00FF00);
return utils;
} }
} }