内存优化: 清理 Activity onDestroy 中的 Companion 静态状态,防止跨实例数据污染
【问题背景】 MainActivity 与 TempCallActivity 都在 companion object 中保存了 Activity 生命周期之外的静态状态。这些字段在 onDestroy 中未全部清理: 1. pendingJsCode: 主线程同步访问的字符串缓冲区,可能持有大量待执行 JS 2. pendingDialog: 跨 Activity 实例缓存的弹窗信息,可能持有旧 Context 引用 3. isConflictDialogShowing: 弹窗标志,Activity 销毁后残留会导致新 Activity 状态错乱 4. TempCallActivity: 同类问题,仅部分清理 (currentGroupName) 【潜在风险】 - Activity 旋转屏/重建时,旧数据可能意外触发新 Activity 的逻辑 - 长期使用中,companion 状态累加,可能造成隐性内存泄漏 - 多 Activity 切换场景下,残留状态可能导致逻辑分支错误 【修复方案】 1. MainActivity.onDestroy 中新增 clearCompanionState() 方法: - pendingJsCode 置 null (在 jsLock 同步块内) - pendingDialog 置 null - isConflictDialogShowing 复位 false 2. 在 instance = null 之前调用,避免清理期间 Native 回调写入冲突 3. TempCallActivity 移除冗余中文注释,保留原有清理逻辑 (已正确) 【兼容性】 - 纯本地状态清理,不影响对外 API - 编译通过 (Kotlin + Java) - 行为变化: Activity 销毁后再次进入不会继承旧状态 (符合预期) 【影响范围】 - MainActivity.kt - TempCallActivity.kt
This commit is contained in:
parent
4465887c96
commit
eaf169c626
|
|
@ -489,10 +489,19 @@ class MainActivity : AppCompatActivity() {
|
|||
geckoView.releaseSession()
|
||||
geckoSession?.close()
|
||||
geckoSession = null
|
||||
clearCompanionState()
|
||||
instance = null
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
private fun clearCompanionState() {
|
||||
synchronized(jsLock) {
|
||||
pendingJsCode = null
|
||||
}
|
||||
pendingDialog = null
|
||||
isConflictDialogShowing = false
|
||||
}
|
||||
|
||||
private fun startTimeoutTimer() {
|
||||
cancelTimeoutTimer()
|
||||
mHandler.postDelayed(timeoutRunnable, TIMEOUT_MS)
|
||||
|
|
|
|||
|
|
@ -114,11 +114,9 @@ class TempCallActivity : AppCompatActivity() {
|
|||
pulseAnimator?.cancel()
|
||||
if (instance == this) {
|
||||
instance = null
|
||||
// 如果不是最小化状态,说明是真正结束,清除所有状态
|
||||
if (!isMinimized) {
|
||||
currentGroupName = "临时会话"
|
||||
}
|
||||
// 通知 web 页面状态变化
|
||||
MainActivity.executeJs("onTempCallStateChanged('" + if (isMinimized) "minimized" else "destroyed" + "')")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue