You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AlertDialog二次展示功能异常,PickFsDone未正确置真求助

Hey Rob, sorry to hear you're hitting this frustrating AlertDialog issue—let's walk through the most likely culprits and fix this together!

First, let's unpack the core problem: your main code is stuck waiting for PickFsDone to be true the second time you show the dialog, even though the dialog closes on screen. Here are the key things to check and fix:

1. You might be blocking the main thread with a waiting loop

If your main code uses something like this to wait for the dialog:

// BAD: Blocks the main thread!
showMyDialog();
while (!PickFsDone) {
    // Empty loop spinning
}

This is a critical mistake. The main thread handles all UI operations—including dialog dismissals and their callbacks. If you block it with a loop, the dialog's onDismiss or button click callbacks can never run to set PickFsDone to true.

Fix: Replace the blocking loop with a callback or observable pattern. For example, use a listener interface:

// Define a callback interface
interface DialogCompletionListener {
    void onDialogCompleted();
}

// Update your dialog function to accept the listener
public void showPickFsDialog(Context context, DialogCompletionListener listener) {
    // Reset state upfront
    PickFsDone = false;

    AlertDialog dialog = new AlertDialog.Builder(context)
        .setTitle("Select FS")
        .setPositiveButton("Confirm", (dialogInterface, i) -> {
            PickFsDone = true;
            listener.onDialogCompleted();
            dialogInterface.dismiss();
        })
        .setNegativeButton("Cancel", (dialogInterface, i) -> {
            PickFsDone = true; // Adjust based on your logic—maybe false?
            listener.onDialogCompleted();
            dialogInterface.dismiss();
        })
        .setOnDismissListener(dialogInterface -> {
            // Catch cases where user taps outside the dialog
            if (!PickFsDone) {
                PickFsDone = true;
                listener.onDialogCompleted();
            }
        })
        .create();

    dialog.show();
}

// Call the dialog like this (no blocking loop!)
showPickFsDialog(this, () -> {
    // Run your post-dialog logic here instead of waiting in a loop
    doSomethingAfterDialog();
});

2. PickFsDone might lack thread visibility or proper resetting

  • Thread visibility: If PickFsDone is a plain boolean and your waiting code runs on a background thread, the thread might not see the update from the main thread's dialog callback. Mark it as volatile to fix this:
    private volatile boolean PickFsDone = false;
    
  • Reset logic: Make sure you set PickFsDone = false every time before you show the dialog. If you forget, the second call might start with PickFsDone already true, but your waiting logic could still get stuck if the callback doesn't fire (see point 1).

3. You're reusing a single AlertDialog instance

If you're storing a global AlertDialog variable and calling show() on it again without rebuilding it, the callbacks might not be reattached correctly. Dialogs are meant to be one-time use in most cases—rebuilding them ensures all listeners are properly bound.

Fix: Always create a new AlertDialog instance each time you call your show function, like in the code example above.

4. Missing dismiss callback coverage

Double-check that all ways of closing the dialog trigger a PickFsDone update:

  • Positive/negative button clicks
  • Tapping outside the dialog (if enabled)
  • Pressing the back button

Adding an OnDismissListener (like in the code example) ensures even unexpected dismissals update your state.

Give these fixes a try—I bet one of them is the root cause of your second-dialog hang!

内容的提问来源于stack exchange,提问作者user2064070

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:10:45