android - 有没有办法在android中显示一个全局对话框?

标签 android android-alertdialog android-application-class

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

  1. 如何记录主机(一直处于 Activity 状态)被销毁或正在完成时的显示时间?

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

最佳答案

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

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

关于android - 有没有办法在android中显示一个全局对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59525267/

相关文章:

android - 停止执行互联网 - 在应用程序子类中

java - Android API-8 抽屉式导航

android - 需要 Gradle 版本 1.10。当前版本是 2.0

android - 长按 ListView 和删除对话框

android - 对象未添加到应用程序类线程内的 ArrayList

android - 应用程序子类实现了所需的接口(interface),但系统拒绝

android - 仅在 Android 浏览器上无法正确显示背景图像

android - 如何禁用自动完成 TextView 更改其文本?

android - Android 应用程序中的对话框显示 : How to set Context out of Activity

android - 如何设置对话框外边距? (安卓)