Skip to content

Commit 6fc50bd

Browse files
committed
修复DialogFragment在打开后立即关闭情况下导致dialog无法关闭的问题
1 parent 4b4b6d4 commit 6fc50bd

File tree

6 files changed

+65
-83
lines changed

6 files changed

+65
-83
lines changed

DialogFragmentUtilsLib/src/main/java/com/osard/dialogfragmentutilslib/BaseDialogLibUtils.java

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,14 @@ public void run() {
5454
if (!fragmentManager.isStateSaved()) {
5555
show(fragmentManager, getAlias());
5656
} else if (!activity.isFinishing() && !activity.isDestroyed()) {
57-
handler.postDelayed(this, 100);
57+
handler.postDelayed(this, RETRY_DELAY_MS);
5858
}
5959
}
6060
};
6161

62+
private static final int MAX_CLOSE_RETRY_COUNT = 30;
63+
private static final long RETRY_DELAY_MS = 100;
64+
6265
@NonNull
6366
public Context getContext() {
6467
return context;
@@ -204,4 +207,60 @@ public void closeDuplicateAliasDialog() {
204207
duplicateAliasClose = true;
205208
closeDialog();
206209
}
210+
211+
/**
212+
* 延迟关闭方法,避免出现还未添加就关闭导致无法关闭的情况,最大延迟3秒
213+
* @param TAG
214+
* @param retryCount
215+
* @return
216+
*/
217+
protected boolean closeDialogWithRetry(String TAG, int retryCount) {
218+
try {
219+
FragmentActivity activity = (FragmentActivity) getContext();
220+
if (activity.isFinishing() || activity.isDestroyed()) {
221+
return false;
222+
}
223+
224+
FragmentManager fragmentManager = activity.getSupportFragmentManager();
225+
226+
// 情况1:已添加且状态未保存 - 直接关闭
227+
if (isAdded() && !fragmentManager.isStateSaved()) {
228+
dismiss(); // 优先用严格模式
229+
return true;
230+
}
231+
// 情况2:未添加但还可以重试 - 延迟重试
232+
else if (retryCount < MAX_CLOSE_RETRY_COUNT) {
233+
handler.postDelayed(() -> {
234+
closeDialogWithRetry(TAG, retryCount + 1);
235+
}, RETRY_DELAY_MS);
236+
return true;
237+
}
238+
// 情况3:已达最大重试次数或状态已保存 - 使用保底方案
239+
else {
240+
dismissAllowingStateLoss(); // 保底方案
241+
return true;
242+
}
243+
} catch (Exception e) {
244+
if (DialogLibInitSetting.getInstance().isDebug()) {
245+
Log.w(TAG, "关闭对话框异常(重试次数: " + retryCount + ")", e);
246+
}
247+
248+
// 异常情况下也尝试重试
249+
if (retryCount < MAX_CLOSE_RETRY_COUNT) {
250+
new Handler(Looper.getMainLooper()).postDelayed(() -> {
251+
closeDialogWithRetry(TAG, retryCount + 1);
252+
}, RETRY_DELAY_MS);
253+
} else {
254+
try {
255+
dismissAllowingStateLoss(); // 最终保底
256+
} catch (Exception finalE) {
257+
if (DialogLibInitSetting.getInstance().isDebug()) {
258+
Log.w(TAG, "最终关闭对话框失败", finalE);
259+
}
260+
return false;
261+
}
262+
}
263+
return true;
264+
}
265+
}
207266
}

DialogFragmentUtilsLib/src/main/java/com/osard/dialogfragmentutilslib/DialogLibAllCustom.java

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@
44
import android.content.DialogInterface;
55
import android.content.res.Configuration;
66
import android.os.Bundle;
7-
import android.util.Log;
87
import android.view.LayoutInflater;
98
import android.view.View;
109
import android.view.ViewGroup;
1110

1211
import androidx.annotation.NonNull;
1312
import androidx.annotation.Nullable;
14-
import androidx.fragment.app.FragmentActivity;
15-
import androidx.fragment.app.FragmentManager;
16-
17-
import com.osard.dialogfragmentutilslib.init.DialogLibInitSetting;
1813

1914
/**
2015
* 全部自定义弹窗提示工具类,需要完全定义dialog的布局
@@ -140,20 +135,7 @@ public DialogLibAllCustom show(View customView) {
140135
}
141136

142137
public boolean closeDialog() {
143-
try {
144-
FragmentActivity activity = (FragmentActivity) getContext();
145-
FragmentManager fragmentManager = activity.getSupportFragmentManager();
146-
if (isAdded() && !fragmentManager.isStateSaved()) {
147-
dismiss(); // 优先用严格模式
148-
} else {
149-
dismissAllowingStateLoss(); // 保底方案
150-
}
151-
} catch (Exception e) {
152-
if (DialogLibInitSetting.getInstance().isDebug()) {
153-
Log.w(TAG, "关闭对话框异常", e);
154-
}
155-
}
156-
return true;
138+
return closeDialogWithRetry(TAG, 0);
157139
}
158140

159141
public interface OnDismissListener {

DialogFragmentUtilsLib/src/main/java/com/osard/dialogfragmentutilslib/DialogLibCommon.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
import androidx.annotation.Nullable;
1717
import androidx.annotation.StringRes;
1818
import androidx.databinding.DataBindingUtil;
19-
import androidx.fragment.app.FragmentActivity;
20-
import androidx.fragment.app.FragmentManager;
2119

2220
import com.osard.dialogfragmentutilslib.databinding.DialogUtilsLibTipBinding;
2321
import com.osard.dialogfragmentutilslib.init.DialogLibInitSetting;
@@ -399,20 +397,7 @@ public DialogLibCommon show() {
399397
}
400398

401399
public boolean closeDialog() {
402-
try {
403-
FragmentActivity activity = (FragmentActivity) getContext();
404-
FragmentManager fragmentManager = activity.getSupportFragmentManager();
405-
if (isAdded() && !fragmentManager.isStateSaved()) {
406-
dismiss(); // 优先用严格模式
407-
} else {
408-
dismissAllowingStateLoss(); // 保底方案
409-
}
410-
} catch (Exception e) {
411-
if (DialogLibInitSetting.getInstance().isDebug()) {
412-
Log.w(TAG, "关闭对话框异常", e);
413-
}
414-
}
415-
return true;
400+
return closeDialogWithRetry(TAG, 0);
416401
}
417402

418403
public interface OnBtnOk {

DialogFragmentUtilsLib/src/main/java/com/osard/dialogfragmentutilslib/DialogLibCustom.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
import androidx.annotation.Nullable;
1515
import androidx.annotation.StringRes;
1616
import androidx.databinding.DataBindingUtil;
17-
import androidx.fragment.app.FragmentActivity;
18-
import androidx.fragment.app.FragmentManager;
1917

2018
import com.osard.dialogfragmentutilslib.databinding.DialogUtilsLibCustomViewBinding;
2119
import com.osard.dialogfragmentutilslib.init.DialogLibInitSetting;
@@ -337,20 +335,7 @@ public DialogLibCustom show(View customView) {
337335
}
338336

339337
public boolean closeDialog() {
340-
try {
341-
FragmentActivity activity = (FragmentActivity) getContext();
342-
FragmentManager fragmentManager = activity.getSupportFragmentManager();
343-
if (isAdded() && !fragmentManager.isStateSaved()) {
344-
dismiss(); // 优先用严格模式
345-
} else {
346-
dismissAllowingStateLoss(); // 保底方案
347-
}
348-
} catch (Exception e) {
349-
if (DialogLibInitSetting.getInstance().isDebug()) {
350-
Log.w(TAG, "关闭对话框异常", e);
351-
}
352-
}
353-
return true;
338+
return closeDialogWithRetry(TAG, 0);
354339
}
355340

356341
public interface OnCustomBtnOk {

DialogFragmentUtilsLib/src/main/java/com/osard/dialogfragmentutilslib/DialogLibInput.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import androidx.annotation.Nullable;
2525
import androidx.annotation.StringRes;
2626
import androidx.databinding.DataBindingUtil;
27-
import androidx.fragment.app.FragmentActivity;
28-
import androidx.fragment.app.FragmentManager;
2927

3028
import com.osard.dialogfragmentutilslib.databinding.DialogUtilsLibTipInputBinding;
3129
import com.osard.dialogfragmentutilslib.init.DialogLibInitSetting;
@@ -494,20 +492,7 @@ protected void onReceiveResult(int resultCode, Bundle resultData) {
494492
}
495493

496494
public boolean closeDialog() {
497-
try {
498-
FragmentActivity activity = (FragmentActivity) getContext();
499-
FragmentManager fragmentManager = activity.getSupportFragmentManager();
500-
if (isAdded() && !fragmentManager.isStateSaved()) {
501-
dismiss(); // 优先用严格模式
502-
} else {
503-
dismissAllowingStateLoss(); // 保底方案
504-
}
505-
} catch (Exception e) {
506-
if (DialogLibInitSetting.getInstance().isDebug()) {
507-
Log.w(TAG, "关闭对话框异常", e);
508-
}
509-
}
510-
return true;
495+
return closeDialogWithRetry(TAG, 0);
511496
}
512497

513498
@Override

DialogFragmentUtilsLib/src/main/java/com/osard/dialogfragmentutilslib/DialogLibLoading.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
import androidx.annotation.StringRes;
1818
import androidx.databinding.DataBindingUtil;
1919
import androidx.fragment.app.DialogFragment;
20-
import androidx.fragment.app.FragmentActivity;
21-
import androidx.fragment.app.FragmentManager;
2220

2321
import com.osard.dialogfragmentutilslib.databinding.DialogUtilsLibLoadingDataBinding;
2422
import com.osard.dialogfragmentutilslib.init.DialogLibInitSetting;
@@ -201,19 +199,7 @@ public DialogLibLoading show() {
201199
}
202200

203201
public boolean closeDialog() {
204-
try {
205-
FragmentActivity activity = (FragmentActivity) getContext();
206-
FragmentManager fragmentManager = activity.getSupportFragmentManager();
207-
if (isAdded() && !fragmentManager.isStateSaved()) {
208-
dismiss(); // 优先用严格模式
209-
} else {
210-
dismissAllowingStateLoss(); // 保底方案
211-
}
212-
} catch (Exception e) {
213-
if (DialogLibInitSetting.getInstance().isDebug()) {
214-
Log.w(TAG, "关闭对话框异常", e);
215-
}
216-
}
202+
closeDialogWithRetry(TAG, 0);
217203
handler.removeCallbacks(timeoutRunnable);
218204
return true;
219205
}

0 commit comments

Comments
 (0)