Thread safe way of displaying a Toast Message For AsyncTask
suggest changeIf you don't want to extend Application and keep your toast messages thread safe, make sure you show them in onPostExecute. context is not declared in this snippet.
public class MyAsyncTask extends AsyncTask <Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
// Do your background work here
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
// Show toast messages here
Toast.makeText(context, "Ding! Your Toast is ready.", Toast.LENGTH_SHORT).show();
}
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents