0

Let's say my App wants to fetch some data from the internet, and it does so through an AsyncTask. When this task finishes, it posts an event with the response data to the EventBus.

The AsyncTask may be triggered either via a Service or an UI. If an UI triggers it, then, before starting the fetch request, a modal dialog is created, indicating that the download is being performed.

How do I manage the lifetime of the dialog? When the result comes in via an EventBus event, I know that I can close the dialog. Should the download fail, I would then need to publish an event specifically for the originator to close the dialog?

Doesn't this coupling somehow diminish the benefit of using EventBus?

How would I go about this, so that I don't have to publish a "close dialog" event? Move the dialog creation/destruction into the AsyncTask?

Is there a pattern that deals with this issue?

Daniel F
  • 13,684
  • 11
  • 87
  • 116
  • Why can't you close the dialog from within the `AsyncTask`? You could pass the originator to the `AsyncTask` using a `WeakReference` and when the download fails or succeeds, close the dialog. – Morgan Apr 10 '18 at 13:54
  • Because the Dialog doesn't always exist. If the download is started while no UI is visible, the AsyncTask shouldn't be bothered with it. It shouldn't know about any UI-related stuff. I will improve the question – Daniel F Apr 10 '18 at 13:57

0 Answers0