android - Android 等待侦听器冻结应用程序?
问题描述
我想显示一个进度对话框并在onCompleteListener
响应如下后将其关闭:
class DialogSubjectsAdd: DialogFragment() {
private val db = FirebaseFirestore.getInstance().collection("courses")
private var docFetched = false
private var processCompleted = false
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
super.onCreateDialog(savedInstanceState)
getCoursesIndexDoc()
// show progress dialog
// wait until download operation is completed
while (!processCompleted) {}
// dismiss dialog
// todo
}
private fun getCoursesIndexDoc() {
// fetch the index document
db.document("all")
.get()
.addOnCompleteListener { task ->
if (task.isSuccessful) {
docFetched = true
}
processCompleted = true
}
}
}
但是上面的代码冻结了应用程序。
如果我将循环注释while
并关闭对话框代码为:
// while (!processCompleted) {}
// // dismiss dialog
进度对话框永远显示。
那么,为什么while
循环会冻结应用程序?
即使processCompleted
never 的值变为true
,我认为它应该导致进度条永远运行而不是冻结应用程序。
但是由于循环和显示剩余点击的按钮并且应用程序被冻结,即使是进度dialog
也没有显示,为什么?while
dialog
解决方案
那是因为onCreateDialog
在系统的 UI 线程上运行 - 这意味着 UI 在某些东西运行时无法更新。
解决方案是移动代码以将对话框关闭到单独的线程 - 您的完成侦听器似乎是完美的地方!
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
super.onCreateDialog(savedInstanceState)
getCoursesIndexDoc()
// Don't do anything else here!
}
private fun getCoursesIndexDoc() {
// fetch the index document
db.document("all")
.get()
.addOnCompleteListener { task ->
if (task.isSuccessful) {
docFetched = true
}
// Instead of setting the flag, dismiss the dialog here
}
}
推荐阅读
- python - sklearn LatentDirichletAllocation 对新语料库的主题推断
- python-3.x - 如何使用 TF-IDF 向量选择前 1000 个单词?
- javascript - 带有 Vue.js 的 ASP.NET Core 不接受我的纯 js 组件
- r - 在闪亮的循环中读取相同的数据帧
- java - 重构刚性代码以消除检查 instanceOf 的 if-else 语句
- django - 如何在 Django 中直接为 OneToOne 关系创建模型
- java - 内部类是否继承到外部类的子类?
- android - 使用 Lottie 文件时 Android Studio 中的 java.io.IOException 错误
- android - 当设备重新启动并强制关闭应用程序时,Redmi 和其他自定义中文 ROM 中未收到 WorkManager doWork 回调
- python - 如何忽略熊猫断言帧相等的索引比较