有什么办法可以在android中显示全局对话框吗?

我需要显示并维护一个对话框,该对话框在一段时间(例如30 s)后在Android应用程序上消失。但是我遇到了一些困难:

  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中的全局对话框?感谢您的收看和回答:)

azsxdcf951357 回答:有什么办法可以在android中显示全局对话框吗?

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

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

本文链接:https://www.f2er.com/2842460.html

大家都在问