I need to show and maintain a dialog that disappears after a certain period of time(example: 30 s) on the Android Application. But I encountered some difficulties:
how to record the showing time when host(is always activity) is destroyed or is Finishing?
how to re-show the dialog when other host resumed if need?
I try it by the following code, but it don't work
// 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.
}
}
Is there any (better )way to show a global dialog in android? Thanks for your watching and answers :)