移除启动页cache缓存,以及移除savedInstanceState 窗体缓存。

This commit is contained in:
2025-12-11 03:18:03 +08:00
parent 6b69e04706
commit bcb5db0a17
2 changed files with 28 additions and 195 deletions

View File

@@ -1,8 +1,8 @@
#Created by .winboll/winboll_app_build.gradle #Created by .winboll/winboll_app_build.gradle
#Thu Dec 11 03:04:35 HKT 2025 #Wed Dec 10 19:15:17 GMT 2025
stageCount=14 stageCount=14
libraryProject= libraryProject=
baseVersion=15.12 baseVersion=15.12
publishVersion=15.12.13 publishVersion=15.12.13
buildCount=0 buildCount=1
baseBetaVersion=15.12.14 baseBetaVersion=15.12.14

View File

@@ -51,15 +51,14 @@ import cc.winboll.studio.powerbell.views.BatteryDrawable;
import cc.winboll.studio.powerbell.views.VerticalSeekBar; import cc.winboll.studio.powerbell.views.VerticalSeekBar;
/** /**
* 主活动类Java 7 兼容 | 首屏加速+窗体缓存) * 主活动类Java 7 兼容 | 移除窗体缓存)
* 核心优化点: * 核心优化点:
* 1. ViewHolder 缓存控件 + ViewStub 延迟加载(原有) * 1. ViewHolder 缓存控件 + ViewStub 延迟加载
* 2. savedInstanceState 窗体缓存(新增):缓存视图状态/数据,快速重建窗体 * 2. 资源异步加载 + 非关键任务异步化
* 3. 资源异步加载 + 非关键任务异步化(原有)
*/ */
public class MainActivity extends WinBoLLActivity { public class MainActivity extends WinBoLLActivity {
// ======================== 静态常量(新增savedInstanceState 缓存键)======================== // ======================== 静态常量(移除缓存相关键)========================
public static final String TAG = "MainActivity"; public static final String TAG = "MainActivity";
private static final int REQUEST_WRITE_STORAGE_PERMISSION = 1001; private static final int REQUEST_WRITE_STORAGE_PERMISSION = 1001;
public static final int MSG_RELOAD_APPCONFIG = 0; public static final int MSG_RELOAD_APPCONFIG = 0;
@@ -67,17 +66,7 @@ public class MainActivity extends WinBoLLActivity {
public static final int MSG_LOAD_BACKGROUND = 2; public static final int MSG_LOAD_BACKGROUND = 2;
private static final int DELAY_LOAD_NON_CRITICAL = 500; private static final int DELAY_LOAD_NON_CRITICAL = 500;
// 缓存键(统一管理,避免键名重复) // ======================== 成员属性(移除缓存标识)========================
private static final String KEY_VIEW_STATE = "key_view_state"; // 视图状态缓存键
private static final String KEY_CHARGE_REMINDER = "key_charge_reminder"; // 充电提醒值
private static final String KEY_USEGE_REMINDER = "key_usege_reminder"; // 耗电提醒值
private static final String KEY_CURRENT_VALUE = "key_current_value"; // 当前电量值
private static final String KEY_IS_SERVICE_ENABLE = "key_is_service_enable"; // 服务开关状态
private static final String KEY_IS_CHARGE_ENABLE = "key_is_charge_enable"; // 充电提醒开关
private static final String KEY_IS_USEGE_ENABLE = "key_is_usege_enable"; // 耗电提醒开关
private static final String KEY_BACKGROUND_BEAN = "key_background_bean"; // 背景配置Bean
// ======================== 成员属性(保持原有,新增缓存标识)========================
public static MainActivity _mMainActivity; public static MainActivity _mMainActivity;
static MainViewFragment _mMainViewFragment; static MainViewFragment _mMainViewFragment;
static Handler _mHandler; static Handler _mHandler;
@@ -99,10 +88,6 @@ public class MainActivity extends WinBoLLActivity {
private BatteryDrawable mChargeReminderValueBatteryDrawable; private BatteryDrawable mChargeReminderValueBatteryDrawable;
private BatteryDrawable mUsegeReminderValueBatteryDrawable; private BatteryDrawable mUsegeReminderValueBatteryDrawable;
// 新增缓存标识判断是否从savedInstanceState恢复避免重复初始化
private boolean isRestoredFromCache = false;
// ======================== 视图缓存容器ViewHolder 不变)======================== // ======================== 视图缓存容器ViewHolder 不变)========================
private static class ViewHolder { private static class ViewHolder {
BackgroundView backgroundView; BackgroundView backgroundView;
@@ -115,8 +100,7 @@ public class MainActivity extends WinBoLLActivity {
ImageView ivCurrentBattery, ivChargeReminderBattery, ivUsegeReminderBattery; ImageView ivCurrentBattery, ivChargeReminderBattery, ivUsegeReminderBattery;
} }
// ======================== 重写生命周期(移除缓存恢复/保存逻辑)========================
// ======================== 重写生命周期核心savedInstanceState 缓存/恢复)========================
@Override @Override
public Activity getActivity() { public Activity getActivity() {
return this; return this;
@@ -129,80 +113,27 @@ public class MainActivity extends WinBoLLActivity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
LogUtils.d(TAG, "onCreate(...) - 窗体缓存优化savedInstanceState = " + (savedInstanceState != null)); LogUtils.d(TAG, "onCreate(...) - 移除窗体缓存");
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
initGlobalHandler(); initGlobalHandler();
// 第一步优先从savedInstanceState恢复窗体核心优化 // 恢复原始初始化流程:直接加载布局+初始化
if (savedInstanceState != null) { setContentView(R.layout.activity_main);
restoreFromCache(savedInstanceState); // 从缓存恢复视图/数据 initCoreUtilsAsync();
isRestoredFromCache = true; // 标记为缓存恢复,跳过重复初始化 initViewHolder();
} initCriticalView();
loadNonCriticalViewDelayed();
// 第二步:无缓存时,正常初始化(保持原有逻辑,新增判断) // 权限申请
if (!isRestoredFromCache) {
setContentView(R.layout.activity_main); // 加载布局(仅首次/无缓存时执行)
initCoreUtilsAsync(); // 异步初始化工具类
initViewHolder(); // 绑定控件(仅首次/无缓存时执行)
initCriticalView(); // 初始化核心视图
loadNonCriticalViewDelayed(); // 延迟加载非核心视图
}
// 第三步:权限申请(无论是否缓存,都需确保权限有效)
PermissionUtils.getInstance().checkAndRequestStoragePermission(this); PermissionUtils.getInstance().checkAndRequestStoragePermission(this);
} }
/** // 移除 onSaveInstanceState 方法
* 重写 onSaveInstanceState保存视图状态/数据到 savedInstanceState(关键) // 移除 onRestoreInstanceState 方法
* 当Activity销毁前如旋转屏幕、后台回收自动调用此方法缓存数据
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
LogUtils.d(TAG, "onSaveInstanceState - 缓存窗体状态/数据");
// 1. 缓存控件数据(避免重建后重复读取配置)
if (mViewHolder != null && mAppConfigUtils != null) {
outState.putInt(KEY_CHARGE_REMINDER, mAppConfigUtils.getChargeReminderValue());
outState.putInt(KEY_USEGE_REMINDER, mAppConfigUtils.getUsegeReminderValue());
outState.putInt(KEY_CURRENT_VALUE, mAppConfigUtils.getCurrentValue());
outState.putBoolean(KEY_IS_SERVICE_ENABLE, mAppConfigUtils.getIsEnableService());
outState.putBoolean(KEY_IS_CHARGE_ENABLE, mViewHolder.cbIsEnableChargeReminder.isChecked());
outState.putBoolean(KEY_IS_USEGE_ENABLE, mViewHolder.cbIsEnableUsegeReminder.isChecked());
}
// 2. 缓存背景配置(避免重建后重复加载背景图)
if (mBgSourceUtils != null) {
BackgroundBean backgroundBean = mBgSourceUtils.getCurrentBackgroundBean();
if (backgroundBean != null) {
outState.putSerializable(KEY_BACKGROUND_BEAN, backgroundBean); // 需BackgroundBean实现Serializable
}
}
// 3. 缓存视图状态可选如SeekBar进度、控件可见性等
if (mViewHolder != null) {
outState.putInt(KEY_VIEW_STATE + "_charge_seek", mViewHolder.chargeReminderSeekBar.getProgress());
outState.putInt(KEY_VIEW_STATE + "_usege_seek", mViewHolder.usegeReminderSeekBar.getProgress());
}
}
/**
* 重写 onRestoreInstanceState从 savedInstanceState 恢复数据可选onCreate中已处理
* 确保在onStart后、onResume前恢复避免视图状态覆盖
*/
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState != null && !isRestoredFromCache) {
restoreFromCache(savedInstanceState);
isRestoredFromCache = true;
}
}
@Override @Override
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
// 释放资源(保持原有)
if (mADsBannerView != null) { if (mADsBannerView != null) {
mADsBannerView.releaseAdResources(); mADsBannerView.releaseAdResources();
} }
@@ -211,23 +142,15 @@ public class MainActivity extends WinBoLLActivity {
if (_mHandler != null) { if (_mHandler != null) {
_mHandler.removeCallbacksAndMessages(null); _mHandler.removeCallbacksAndMessages(null);
} }
// 重置缓存标识
isRestoredFromCache = false;
} }
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
// 缓存恢复后,无需重复发送加载消息(仅首次/无缓存时执行) // 移除缓存恢复后的刷新逻辑,直接发送加载背景消息
if (!isRestoredFromCache) { if (_mHandler != null) {
if (_mHandler != null) { _mHandler.sendEmptyMessage(MSG_LOAD_BACKGROUND);
_mHandler.sendEmptyMessage(MSG_LOAD_BACKGROUND);
}
} else {
// 缓存恢复后,快速刷新视图(避免状态不一致)
refreshViewFromCache();
} }
// 恢复广告(保持原有)
if (mADsBannerView != null) { if (mADsBannerView != null) {
mADsBannerView.resumeADs(MainActivity.this); mADsBannerView.resumeADs(MainActivity.this);
} }
@@ -312,94 +235,10 @@ public class MainActivity extends WinBoLLActivity {
} }
} }
// ======================== 移除缓存相关核心方法restoreFromCache/refreshViewFromCache========================
// ======================== 新增:窗体缓存核心方法(恢复+刷新)========================
/**
* 从 savedInstanceState 恢复视图和数据(核心)
* 避免重复加载布局、绑定控件、读取配置,加快窗体重建速度
*/
private void restoreFromCache(Bundle savedInstanceState) {
LogUtils.d(TAG, "restoreFromCache - 从缓存恢复窗体");
// 1. 恢复工具类(无需重新初始化,直接获取实例)
mApplication = (App) getApplication();
mAppConfigUtils = App.getAppConfigUtils(getActivity());
mBgSourceUtils = BackgroundSourceUtils.getInstance(getActivity());
// 2. 恢复ViewHolder仅绑定控件不重新inflate布局
if (mViewHolder == null) {
mViewHolder = new ViewHolder();
initViewHolder(); // 复用原有绑定逻辑,避免重复代码
}
// 3. 恢复背景配置直接设置缓存的BackgroundBean避免重复读取
BackgroundBean cachedBean = (BackgroundBean) savedInstanceState.getSerializable(KEY_BACKGROUND_BEAN);
if (cachedBean != null && mViewHolder.backgroundView != null) {
mViewHolder.backgroundView.loadBackgroundBean(cachedBean);
//mBgSourceUtils.setCurrentBackgroundBean(cachedBean); // 同步更新工具类缓存
}
// 4. 恢复控件数据从缓存读取避免重复调用AppConfigUtils
int chargeReminder = savedInstanceState.getInt(KEY_CHARGE_REMINDER, 80); // 默认值80
int usegeReminder = savedInstanceState.getInt(KEY_USEGE_REMINDER, 20); // 默认值20
int currentValue = savedInstanceState.getInt(KEY_CURRENT_VALUE, 50); // 默认值50
boolean isServiceEnable = savedInstanceState.getBoolean(KEY_IS_SERVICE_ENABLE, false);
boolean isChargeEnable = savedInstanceState.getBoolean(KEY_IS_CHARGE_ENABLE, false);
boolean isUsegeEnable = savedInstanceState.getBoolean(KEY_IS_USEGE_ENABLE, false);
// 5. 恢复电量图标(复用缓存实例,避免重新创建)
initBatteryDrawables();
mCurrentValueBatteryDrawable.setValue(currentValue);
mChargeReminderValueBatteryDrawable.setValue(chargeReminder);
mUsegeReminderValueBatteryDrawable.setValue(usegeReminder);
// 6. 恢复控件状态(直接设置,避免重复绑定数据)
if (mViewHolder != null) {
mViewHolder.cbIsEnableChargeReminder.setChecked(isChargeEnable);
mViewHolder.cbIsEnableUsegeReminder.setChecked(isUsegeEnable);
mViewHolder.swIsEnableService.setChecked(isServiceEnable);
mViewHolder.chargeReminderSeekBar.setProgress(savedInstanceState.getInt(KEY_VIEW_STATE + "_charge_seek", chargeReminder));
mViewHolder.usegeReminderSeekBar.setProgress(savedInstanceState.getInt(KEY_VIEW_STATE + "_usege_seek", usegeReminder));
mViewHolder.tvChargeReminderValue.setText(String.valueOf(chargeReminder) + "%");
mViewHolder.tvUsegeReminderValue.setText(String.valueOf(usegeReminder) + "%");
mViewHolder.tvCurrentValue.setText(String.valueOf(currentValue) + "%");
}
// 7. 恢复非核心视图(广告)
loadAdsView();
}
/** /**
* 缓存恢复后,快速刷新视图(确保状态一致 * 初始化电量图标(复用逻辑
*/
private void refreshViewFromCache() {
if (mViewHolder == null) return;
// 快速设置背景色从缓存Bean获取
if (mBgSourceUtils != null && mViewHolder.mainLayout != null) {
BackgroundBean cachedBean = mBgSourceUtils.getCurrentBackgroundBean();
if (cachedBean != null) {
mViewHolder.mainLayout.setBackgroundColor(cachedBean.getPixelColor());
}
}
// 快速设置电量图标(避免图标不显示)
if (mViewHolder.ivCurrentBattery != null) {
mViewHolder.ivCurrentBattery.setImageDrawable(mCurrentValueBatteryDrawable);
}
if (mViewHolder.ivChargeReminderBattery != null) {
mViewHolder.ivChargeReminderBattery.setImageDrawable(mChargeReminderValueBatteryDrawable);
}
if (mViewHolder.ivUsegeReminderBattery != null) {
mViewHolder.ivUsegeReminderBattery.setImageDrawable(mUsegeReminderValueBatteryDrawable);
}
// 重新绑定监听(确保交互正常,避免监听失效)
setViewListener();
}
/**
* 初始化电量图标(复用逻辑,避免重复代码)
*/ */
private void initBatteryDrawables() { private void initBatteryDrawables() {
if (mCurrentValueBatteryDrawable == null) { if (mCurrentValueBatteryDrawable == null) {
@@ -413,13 +252,11 @@ public class MainActivity extends WinBoLLActivity {
} }
} }
// ======================== 原有核心方法(移除缓存判断逻辑)========================
// ======================== 原有核心方法(微调:新增缓存判断,避免重复执行)========================
/** /**
* 刷新背景(新增缓存判断,缓存恢复后无需重复执行 * 刷新背景(移除缓存判断)
*/ */
public void reloadBackground() { public void reloadBackground() {
if (isRestoredFromCache) return; // 缓存恢复后,跳过重复加载
if (mViewHolder == null || mBgSourceUtils == null || mViewHolder.backgroundView == null) { if (mViewHolder == null || mBgSourceUtils == null || mViewHolder.backgroundView == null) {
LogUtils.e(TAG, "reloadBackground: 背景加载失败(控件/工具类未初始化)"); LogUtils.e(TAG, "reloadBackground: 背景加载失败(控件/工具类未初始化)");
return; return;
@@ -434,10 +271,9 @@ public class MainActivity extends WinBoLLActivity {
} }
/** /**
* 设置主页面背景颜色(新增缓存判断) * 设置主页面背景颜色(移除缓存判断)
*/ */
void setBackgroundColor() { void setBackgroundColor() {
if (isRestoredFromCache) return;
if (isFinishing() || isDestroyed() || mViewHolder == null || mBgSourceUtils == null || mViewHolder.mainLayout == null) { if (isFinishing() || isDestroyed() || mViewHolder == null || mBgSourceUtils == null || mViewHolder.mainLayout == null) {
return; return;
} }
@@ -449,10 +285,9 @@ public class MainActivity extends WinBoLLActivity {
} }
/** /**
* 设置视图数据(新增缓存判断,缓存恢复后无需重复执行 * 设置视图数据(移除缓存判断)
*/ */
void setViewData() { void setViewData() {
if (isRestoredFromCache) return;
if (mViewHolder == null || mAppConfigUtils == null) return; if (mViewHolder == null || mAppConfigUtils == null) return;
int nChargeReminderValue = mAppConfigUtils.getChargeReminderValue(); int nChargeReminderValue = mAppConfigUtils.getChargeReminderValue();
@@ -464,7 +299,7 @@ public class MainActivity extends WinBoLLActivity {
mViewHolder.llRightSeekBar.setBackground(mDrawableFrame); mViewHolder.llRightSeekBar.setBackground(mDrawableFrame);
} }
initBatteryDrawables(); // 复用初始化逻辑 initBatteryDrawables();
if (mViewHolder.ivCurrentBattery != null) { if (mViewHolder.ivCurrentBattery != null) {
mCurrentValueBatteryDrawable.setValue(nCurrentValue); mCurrentValueBatteryDrawable.setValue(nCurrentValue);
mViewHolder.ivCurrentBattery.setImageDrawable(mCurrentValueBatteryDrawable); mViewHolder.ivCurrentBattery.setImageDrawable(mCurrentValueBatteryDrawable);
@@ -478,7 +313,6 @@ public class MainActivity extends WinBoLLActivity {
mViewHolder.ivUsegeReminderBattery.setImageDrawable(mUsegeReminderValueBatteryDrawable); mViewHolder.ivUsegeReminderBattery.setImageDrawable(mUsegeReminderValueBatteryDrawable);
} }
// 控件数据绑定(保持原有)
if (mViewHolder.tvChargeReminderValue != null) { if (mViewHolder.tvChargeReminderValue != null) {
mViewHolder.tvChargeReminderValue.setTextColor(getActivity().getColor(R.color.colorCharge)); mViewHolder.tvChargeReminderValue.setTextColor(getActivity().getColor(R.color.colorCharge));
mViewHolder.tvChargeReminderValue.setText(String.valueOf(nChargeReminderValue) + "%"); mViewHolder.tvChargeReminderValue.setText(String.valueOf(nChargeReminderValue) + "%");
@@ -515,7 +349,6 @@ public class MainActivity extends WinBoLLActivity {
} }
} }
// 其他原有方法setViewListener、setCurrentValueBattery、initCoreUtilsAsync等保持不变
/** /**
* 设置视图监听(保持原有) * 设置视图监听(保持原有)
*/ */