ProgressDialog.show(getApplicationContext(), ...); // 拋出 BadTokenException
Dialog 依附於 Window,需要 Activity 類型的 Context 提供窗口令牌。Application Context 無窗口管理能力,無法作為 Dialog 宿主。
ProgressDialog.show(MainActivity.this, ...)requireActivity() 或 getActivity()。
3. 非 UI 線程回調:用 Handler(Looper.getMainLooper()) 切回主線程再用 Activity Context 顯示。
new Handler().postDelayed(() -> {
ProgressDialog.show(getApplicationContext(), ...); // 仍會崩,不可行
}, 300);
根本解決是使用正確 Context,而非延遲。
使用 DialogFragment 管理進度對話框,生命周期自動綁定,避免窗口泄漏和 Context 問題。