I am downloading a file from the Azure storage and showing an alert dialog to a user with a progress bar. I am downloading the file from the background thread and showing the alert on the UI thread. When I start downloading the file it is showing the alert dialog to the progress bar and when the user presses the home button and came to the app and the alert dialog is not visible to the user. I am getting instance of alert dialog but it is not showing.
How my theard look like:
private fun startTheard() {
val thread = Thread {
// downloading and calling this method to show alert dialog
showDialogBox(10)
}
thread.start()
}
When the user came back to the application and I put debugger and theard and it is calling the showDialogBox
function with the updated progress but it is not visible to the user.
How I am generating alert dialog:
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
}
}
}
Alert dialog is global instace of activity :
class MainActivity : AppCompatActivity() {
private var alertDialog: AlertDialog? = null
one observation is when a user came back to the app and I checked in the onResume
alert dialog instance is null and when the showDialogBox
function gets called from the background thread at that time alert dialog instance is not null.
NOTE: I have also tried to dismiss the dialog in the onPause
method but still facing the same issue.
How do I solve this? Am I missing anything?