How do I display a dialog in android without an Activity context?

If an error comes back, I want to display an error dialog.

Please only do this if you know that the user is actively using your application. The user will be very very annoyed if you interrupt them in the middle of something else (playing a game, watching a movie, reading a book).

So how can I get the context of the current activity?

You don’t. At most, you let the current activity know that it needs to do something.

Any other ideas?

One possibility is to use an ordered broadcast, so if you have a foreground activity, it gets control, otherwise you raise a Notification to let the user know about the problem without popping a dialog. The activity that receives the ordered broadcast can display an AlertDialog or otherwise let the user know about the problem. I wrote about the details of how to do this in a blog post (and a book chapter, for that matter), and here is a sample application demonstrating the technique.

Or, have the service call startActivity() to start up a dialog-themed activity.

Leave a Comment