首页 > 解决方案 > 运行函数,直到另一个完成 Android

问题描述

我想执行一个函数本身n次,这个函数负责显示一个对话框,但目前同时生成了那个数量的对话框,我希望显示那个对话框,一旦它关闭就再次打开下一个,而不是一举一动。

对此有何建议?

这是我这个函数的主要代码

private fun onReportNotification(){
    showMessageDialog(ConfirmationDialog.DIALOG_REPORT_CREATED, getString(R.string.service_line_popup_push_title), getString(R.string.service_line_popup_push_body), getString(R.string.login_password_btn2),"")
}

override fun onResume() {
    super.onResume()
    checkForPendingTicketsToEvaluate()
}

private fun checkForPendingTicketsToEvaluate(){
    ServiceLineRepository.getAllReportsNotAualfied(applicationContext, serverClient){
        boolean: Boolean, unQualifiedReports: ArrayList<Entity.ServiceRequestedElement> ->
        if(boolean){
            unQualifiedReports.forEach {
                async {
                    currentId = it.id
                    currentService = it.requestedService
                    onReportNotification()
                }
            }
        } else {

        }
    }
}

这是 showMessageDialog() 的片段

open fun showMessageDialog(
    dialogType: String,
    title: String,
    body: String,
    acceptBtn: String,
    cancelBtn: String
) {
    Log.d(TAG, "showErrorDialog started, body=$body")
    val ft = supportFragmentManager.beginTransaction()
    val prev = supportFragmentManager.findFragmentByTag(ConfirmDialog)
    if (prev != null) {
        ft.remove(prev)
    }
    ft.addToBackStack(null)
    val arg = Bundle()
    arg.putString(ConfirmationDialog.DIALOG_TYPE_KEY, dialogType)
    arg.putString(ConfirmationDialog.DIALOG_TEXT_BODY_KEY, body)
    arg.putString(ConfirmationDialog.DIALOG_TEXT_TITLE_KEY, title)
    arg.putString(ConfirmationDialog.DIALOG_TEXT_ACEEPT_BTN, acceptBtn)
    arg.putString(ConfirmationDialog.DIALOG_TEXT_CANCEL_BTN, cancelBtn)
    val newFragment = ConfirmationDialog.newInstance(arg)
    try {
        ft.let { newFragment.show(it, ConfirmDialog) }
        Log.d(TAG, "showMessageDialog, showing dialog")
    } catch (e: Exception) {
        e.printStackTrace()
        e.message?.let {
            Log.e(TAG, it)
        }
    }
}

谢谢

标签: javaandroidkotlin

解决方案


这是我能想到的,可能不是最佳实践。但我认为它会解决你的问题。

首先,将您的数组大小传递给onReportNotification()函数并将其从循环中移出,如下所示

        if(boolean){
            unQualifiedReports.forEach {
                async {
                    currentId = it.id
                    currentService = it.requestedService
                }
            }
            onReportNotification(unQualifiedReports.size)
        } else {

        }

然后,在函数中添加数组大小参数onReportNotification()也将其传递给showMessageDialog()方法

private fun onReportNotification(totalDialogToBeShown: Int){
    showMessageDialog(ConfirmationDialog.DIALOG_REPORT_CREATED, getString(R.string.service_line_popup_push_title), getString(R.string.service_line_popup_push_body), getString(R.string.login_password_btn2),"", totalDialogToBeShown)
}

最后,在您的showMessageDialog()功能中,将其更改为此

open fun showMessageDialog(
    dialogType: String,
    title: String,
    body: String,
    acceptBtn: String,
    cancelBtn: String,
    totalDialogToBeShown: Int
) {
    Log.d(TAG, "showErrorDialog started, body=$body")
    val ft = supportFragmentManager.beginTransaction()
    val prev = supportFragmentManager.findFragmentByTag(ConfirmDialog)
    if (prev != null) {
        ft.remove(prev)
    }
    ft.addToBackStack(null)
    val arg = Bundle()
    arg.putString(ConfirmationDialog.DIALOG_TYPE_KEY, dialogType)
    arg.putString(ConfirmationDialog.DIALOG_TEXT_BODY_KEY, body)
    arg.putString(ConfirmationDialog.DIALOG_TEXT_TITLE_KEY, title)
    arg.putString(ConfirmationDialog.DIALOG_TEXT_ACEEPT_BTN, acceptBtn)
    arg.putString(ConfirmationDialog.DIALOG_TEXT_CANCEL_BTN, cancelBtn)
    val newFragment = ConfirmationDialog.newInstance(arg)
    var numberOfShownDialog = 1
    newFragment.onDismiss(object : DialogInterface {
            override fun dismiss() {
                if (numberOfShownDialog < totalDialogToBeShown) {
                    numberOfShownDialog++
                    ft.let { newFragment.show(it, ConfirmDialog) }
                }
            }

            override fun cancel() {}
    })

    try {
        ft.let { newFragment.show(it, ConfirmDialog) }
        Log.d(TAG, "showMessageDialog, showing dialog")
    } catch (e: Exception) {
        e.printStackTrace()
        e.message?.let {
            Log.e(TAG, it)
        }
    }
}

希望这可以解决您的问题:)


推荐阅读