jul
jul

Reputation: 37484

How to send a Toast or any user notification when no more context is available?

I have a Fragment in which I use an AsyncTask to send a tweet. I send the tweet in doInBackground, and when it's sent, onPostExecute is called and I'd like to show a Toast (or any notification) to the user.

The issue is that if the Fragment is paused (e.g. the user changed screens during the twitter request), no more context is available to call

Toast.makeText(Context context, CharSequence text, int duration)

I'm aware of other similar questions, like this one, but I couldn't find any answer...

... and keeping a local copy of the context seems very wrong.

Any suggestion?

Upvotes: 1

Views: 165

Answers (1)

MH.
MH.

Reputation: 45503

As per earlier comment:

You can extend the Application class and (since it's already a singleton) set it up with a singleton accessor. Toasting on the application context will work just fine. If you feel like something more fancy, you can always set up your own singleton which keeps a reference to the application context for toasting messages. See also here.

Upvotes: 1

Related Questions