Reputation: 1443
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:
message produced from a closing activity, eg activity Master start activity Selector, the user selects an option, the code do some stuff and then go back to Master
// Selector activity draft sample
mButtonSeelcted.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
super.onClick(view);
doMagicForSelected();
mContext.sendBroadcast(intentForUserMessage);
mContext.startActivity(intentToMasterActivity);
}
});
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
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
Reputation: 10235
Upvotes: 1