2

我需要在 Android 应用程序上显示并维护一个在一定时间(例如:30 秒)后消失的对话框。但是我遇到了一些困难:</p>

  1. 如何记录主机(始终是活动)被破坏或正在完成的显示时间?

  2. 如果需要,当其他主机恢复时如何重新显示对话框?

我用下面的代码试了一下,还是不行

// dialog that I want to show.
class BusinessBroadcastDialog(activity: Activity, private val tag: String) : AlertDialog(activity)

object GlobalShowableManager {

    fun show(duration: Int) {
        // ActivityRecorder.get() will return the activity which is on the top of task.
        val activity = ActivityRecorder.get()
        if (activity?.isActivityExist() == true) {
            val tag = "${activity.javaClass.simpleName}#BusinessBroadcastDialog#$duration"
            val dialog = BusinessBroadcastDialog(activity, tag).apply {
                ownerActivity = activity
            }
            dialog.show()
        } else {
            Log.i(TAG, "no exist activity to show global dialog, break.")
        }
    }

    fun resumeToShow() {
        // will be called when other host resumed.
        // get last BusinessBroadcastDialog showing time mark it as t. 
        // if t >= duration then do nothing, 
        // else let t = t - duration and show dialog. dialog will disappear after t seconds.
    }
}

有没有(更好的)方式在android中显示全局对话框?感谢您的观看和回答:)

4

1 回答 1

0

实现全局对话框的最简单方法是使用一个活动和多个片段架构,然后每个片段都是新屏幕,您可以使用活动控制对话框。

根据您给出的示例,创建一个私有静态变量以在其中节省时间并在显示对话框时更新计时器,并且在活动的onResume()中,您可以调用resumeToShow()并检查时间并在需要时显示对话框或不

于 2019-12-30T04:14:25.910 回答