android - 当用户从背景进入应用程序时,警报 dailog 不可见
问题描述
我正在从 Azure 存储下载一个文件,并向用户显示一个带有进度条的警报对话框。我正在从后台线程下载文件并在 UI 线程上显示警报。当我开始下载文件时,它会在进度条上显示警报对话框,当用户按下主页按钮并来到应用程序时,用户看不到警报对话框。我正在收到警报对话框的实例,但它没有显示。
我的耳朵是什么样子的:
private fun startTheard() {
val thread = Thread {
// downloading and calling this method to show alert dialog
showDialogBox(10)
}
thread.start()
}
当用户回到应用程序时,我放置了调试器和 theard,它正在调用showDialogBox
具有更新进度的函数,但用户看不到它。
我如何生成警报对话框:
private fun showDialogBox(progress: Int) {
runOnUiThread {
// hear i am getting alertDialog instance i.e it is not null and calling else part
// but not visible to user
if (alertDialog == null) {
val mDialogView = LayoutInflater.from(this).inflate(
R.layout.cdfw_download_status_layout,
null
)
val mBuilder = AlertDialog.Builder(this)
.setView(mDialogView)
.setCancelable(false)
headingTv = mDialogView.findViewById(R.id.heading)
headingSubTv = mDialogView.findViewById(R.id.heading_sub)
closeButtonCdfw = mDialogView.findViewById(R.id.close_button)
cdfwProgressBarForCdfwDilog = mDialogView.findViewById(R.id.progress_bar)
cdfwProgressBarForCdfwDilog.progress = progress
alertDialog = mBuilder.show()
} else {
cdfwProgressBarForCdfwDilog.progress = progress
}
}
}
警报对话框是活动的全局实例:
class MainActivity : AppCompatActivity() {
private var alertDialog: AlertDialog? = null
一个观察结果是,当用户回到应用程序时,我检查了onResume
警报对话框实例是否为空,并且当时showDialogBox
从后台线程调用该函数时,警报对话框实例不为空。
注意:我也尝试关闭该onPause
方法中的对话框,但仍然面临同样的问题。
我该如何解决这个问题?我错过了什么吗?
解决方案
推荐阅读
- python - 逐行迭代与逐列迭代
- ios - UITableView 可见单元格在自动布局后计算视图的位置
- parallel-processing - 用于蛮力装箱并行化的第 N 次排列算法(包含多次相同的项目)
- php - Symfony/Http-foundation 组件如何处理 Http 请求和响应
- php - 按分数和用户名对多维数组进行排序
- android - 在 RecyclerView 中显示原生广告
- android - 如何在 Kotlin 中将 HashMap 转换为 JSON
- android - 阻止 BottomNavigationView 在单击时增加项目的字体大小
- laravel - 如何根据常用标签对相关文章进行排序?
- opencv - 如何通过 emscripten / wasm 在电子中运行 OpenCv