Skip to content

Commit 5bb8a23

Browse files
committed
fix: 修复界面更新相关的崩溃问题
- 在 `DownloadsScene` 中,添加了 `isAdded()` 检查,以避免在 Fragment 未附加到 Activity 时更新适配器或处理回调,从而防止崩溃。 - 在 `DownloadAdapter` 中,增强了 `LayoutInflater` 的获取逻辑。在获取失败时,会尝试从 Context 或 Activity 中获取,以防止因 Fragment 状态异常而导致的 `NullPointerException` 或 `IllegalStateException`。 - 在 `UpdateDialog` 中,增加了对 Activity 生命周期的检查(`isFinishing`/`isDestroyed`),以确保只在 Activity 处于活动状态时才显示对话框,避免 `WindowLeaked` 异常。
1 parent f97da15 commit 5bb8a23

3 files changed

Lines changed: 42 additions & 4 deletions

File tree

app/src/main/java/com/hippo/ehviewer/ui/dialog/UpdateDialog.kt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,16 @@ class UpdateDialog(private val activity: Activity) {
4141
private var myDownloadId by Delegates.notNull<Long>()
4242
private var downloadReceiver: DownloadReceiver? = null
4343

44+
private fun isActivityAlive(): Boolean {
45+
return !(activity.isFinishing || activity.isDestroyed)
46+
}
4447

4548
fun showCheckFailDialog() {
4649
try {
4750
ContextCompat.getMainExecutor(activity).execute {
51+
if (!isActivityAlive()) {
52+
return@execute
53+
}
4854
val alertDialog = AlertDialog.Builder(activity)
4955
.setIcon(R.mipmap.ic_launcher)
5056
.setTitle(R.string.update_fail)
@@ -56,7 +62,9 @@ class UpdateDialog(private val activity: Activity) {
5662
dialog.dismiss()
5763
}
5864
.create()
59-
alertDialog.show()
65+
if (isActivityAlive()) {
66+
alertDialog.show()
67+
}
6068
}
6169
} catch (e: Exception) {
6270
Analytics.recordException(e)
@@ -78,6 +86,9 @@ class UpdateDialog(private val activity: Activity) {
7886

7987
val downloadUrl = updateContent.getString(AppUpdater.FILE_DOWNLOAD_URL)
8088
ContextCompat.getMainExecutor(activity).execute {
89+
if (!isActivityAlive()) {
90+
return@execute
91+
}
8192
val alertDialog = AlertDialog.Builder(activity).apply {
8293
setIcon(R.mipmap.ic_launcher)
8394
setTitle(title)
@@ -95,7 +106,9 @@ class UpdateDialog(private val activity: Activity) {
95106
setCancelable(false)
96107
}
97108
}.create()
98-
alertDialog.show()
109+
if (isActivityAlive()) {
110+
alertDialog.show()
111+
}
99112
}
100113
} catch (e: Exception) {
101114
Analytics.recordException(e)

app/src/main/java/com/hippo/ehviewer/ui/scene/download/DownloadsScene.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,10 @@ private void gotoFilterAndSort(int id) {
13261326
}
13271327

13281328
private void updateAdapter() {
1329+
// 检查 Fragment 是否已附加,如果未附加则延迟创建适配器
1330+
if (!isAdded()) {
1331+
return;
1332+
}
13291333
mOriginalAdapter = new DownloadAdapter(this, this);
13301334
mOriginalAdapter.setHasStableIds(true);
13311335
// 避免重复创建包装适配器,直接使用原始适配器
@@ -1366,6 +1370,10 @@ public boolean forceShowSearchBar() {
13661370

13671371
@Override
13681372
public void onDownloadSearchSuccess(List<DownloadInfo> list) {
1373+
// 检查 Fragment 是否已附加,如果未附加则忽略回调
1374+
if (!isAdded()) {
1375+
return;
1376+
}
13691377
mList = list;
13701378
updateAdapter();
13711379
mProgressView.setVisibility(View.GONE);
@@ -1378,6 +1386,10 @@ public void onDownloadSearchSuccess(List<DownloadInfo> list) {
13781386

13791387
@Override
13801388
public void onDownloadListHandleSuccess(List<DownloadInfo> list) {
1389+
// 检查 Fragment 是否已附加,如果未附加则忽略回调
1390+
if (!isAdded()) {
1391+
return;
1392+
}
13811393
mList = list;
13821394
updateAdapter();
13831395
mProgressView.setVisibility(View.GONE);

app/src/main/java/com/hippo/ehviewer/ui/scene/download/part/DownloadAdapter.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.hippo.ehviewer.ui.scene.download.part;
1818

1919
import android.annotation.SuppressLint;
20+
import android.app.Activity;
2021
import android.content.Context;
2122
import android.content.Intent;
2223
import android.content.res.Resources;
@@ -112,8 +113,20 @@ public DownloadAdapter(DownloadsScene scene, DownloadAdapterCallback callback) {
112113
LayoutInflater mInflater1;
113114
try {
114115
mInflater1 = scene.getLayoutInflater2();
115-
} catch (NullPointerException e) {
116-
mInflater1 = scene.getLayoutInflater();
116+
} catch (NullPointerException | IllegalStateException e) {
117+
// Fragment 可能还未附加到 FragmentManager,使用 Context 获取 LayoutInflater
118+
Context context = scene.getContext();
119+
if (context != null) {
120+
mInflater1 = LayoutInflater.from(context);
121+
} else {
122+
// 如果 Context 也为 null,尝试使用 Activity
123+
Activity activity = scene.getActivity();
124+
if (activity != null) {
125+
mInflater1 = LayoutInflater.from(activity);
126+
} else {
127+
throw new IllegalStateException("Cannot get LayoutInflater: Fragment is not attached and Context/Activity is null");
128+
}
129+
}
117130
}
118131
mInflater = mInflater1;
119132
AssertUtils.assertNotNull(mInflater);

0 commit comments

Comments
 (0)