首页 > 解决方案 > 当用户从背景进入应用程序时,警报 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方法中的对话框,但仍然面临同样的问题。

我该如何解决这个问题?我错过了什么吗?

标签: androidkotlinandroid-alertdialogandroid-backgroundandroid-thread

解决方案


推荐阅读