gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:53:59 25 4
gpt4 key购买 nike

我需要在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com