I know this question has been asked multiple times, but I have an apparently clean code that just doesn’t work, giving me no Exceptions or anything.
I have a trivial one-button Activity (MainActivity). The button in it calls an AsyncTask in order to send an email in the background. I tried to do what I guess it’s a pretty common thing: show a ProgressDialog when the task starts and dismiss it when it ends. To do so I put the above-mentioned code into AsyncTask.onPreExecute() and AsyncTask.onPostExecute().
I thought the problem was in the Context provided to the dialog constructor, but I created a simple constructor for my AsyncTask to which I pass the application context. I added a simple Toast to debug, but it doesn’t show up neither…
Here’s the code for the button onClick method:
public void onClick(View v) {
new Sender(this).execute("args");
}
And here’s the code for the task:
private class Sender extends AsyncTask<String, Void, Void> {
private ProgressDialog progDialog;
private Context context;
public Sender(Context context) {
this.context = context;
}
@SuppressWarnings("unused")
protected void onPreExecute(Void... params) {
Toast.makeText(this.context, "Sending...", Toast.LENGTH_SHORT).show();
progDialog = ProgressDialog.show(this.context, "Sending", "Picture is being sent...", true);
}
protected Void doInBackground(String... mailSubj) {
// some code that works
return null;
}
@SuppressWarnings("unused")
protected void onPostExecute(Void... v) {
progDialog.dismiss();
Toast.makeText(MainActivity.this, "Mail sent", Toast.LENGTH_SHORT).show();
}
}
Where did I go wrong?
Your progress dialog not show up because you don’t override
onPreExecute(). Add@Overrideannotation to youronPreExecute(), see what happens.onPreExecutetakes no arguments.