denispyr
denispyr

Reputation: 1443

User notification needs context

In order to notify a user I use an AlertDialog or a Snanckbar. They both need an Activity context in order to display. Three cases:

Now the message is not displayed because by the time the receiver kicks in, the Selector activity is finished. Plus it gives a WindowLeaked exception as the AlertDialog is never dismissed.

Any thoughts? Is there a pattern I am missing?

Upvotes: 1

Views: 48

Answers (2)

CommonsWare
CommonsWare

Reputation: 1007494

I am implementing a broadcast/receiver pattern so the thread can broadcast the message and the activity receives and shows the message

Please use an in-process message bus (e.g., LocalBroadcastManager, greenrobot's EventBus). Using system broadcasts not only wastes CPU and battery, but it also introduces security issues (e.g., any app can spy on your messages).

Is there a pattern I am missing?

In your third scenario, it is the responsibility of the "master activity" to show this information, not the activity that is being destroyed. So, add information to the Intent that you pass to startActivity() that tells the "master activity" to show that information.

Upvotes: 1

Bertram Gilfoyle
Bertram Gilfoyle

Reputation: 10235

  1. Start the Selector activity (activity B) from Master activity (activity A) using startActivityForResult().
  2. Display the dialog from Master activity as per the result.

Upvotes: 1

Related Questions