2

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:

  1. how to record the showing time when host(is always activity) is destroyed or is Finishing?

  2. 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 :)

ZSpirytus
  • 339
  • 2
  • 10
  • you can use `android.arch.lifecycle` and get the application lifecycle onAppResumed() and onAppStopped().Along with CountDownTimer you can show dialog, but when app stops you can cancel countDownTimer – Shadow Droid Dec 30 '19 at 07:23
  • 1
    @Shadow Droid Thanks for your answer, your answer save me a lot. Finally I solve this problem by emitted an event which record showing time and other extra data to all exist activity. when an exist activity received this event, I add a view(look like a dialog) to the rootView of this activity, and this view will disappear when showing time become zero. It works. – ZSpirytus Dec 30 '19 at 12:05

1 Answers1

0

The easiest way you can achieve a global dialog is to use one activity and multiple fragments architecture, then each fragment is new screen and you can control the dialog using activity.

According to the example you have given create a private static variable to save time in it and update the timer when dialog is displayed and in the onResume() of activity you can call the resumeToShow() and check the time and display the dialog if required or not

  • Thanks for your answer. My Android Application has more than one Activity. so I afriad your suggest will not work. – ZSpirytus Dec 30 '19 at 11:59