I have an Activity “A1” and a ListActivity “A2”
I take inputs from user in Activity “A1” and depending on the inputs given by user I process and generate a result list. The list generated will then be shown in my ListActivity “A2”.
Now, processing the inputs and generating the result list takes a bit too long. So I want to show a loading dialog or loading image or loading message in this time so the user won’t think that the app is stuck up doing nothing.
Now the things I tried so far:
Yes, I tried AsyncTask! (I know many people are about to suggest it) So now I am doing all my processing in an AsyncTask. I am showing a ProgressDialog in onPreExecute, doing my processing in doInBackground and dismissing the dialog onPostExecute.
However, the dialog comes just before the ListActivity “A2” starts. Until then the Activity “A1” just seems to be stuck up!
I want the dialog to show up right when I click the “Submit” button on Activity “A1” and continue to be seen until ListActivity “A2” shows up.
Please let me know if you need anymore details. Thanks.
–EDIT–
Code in onClick method:
ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Processing... Please Wait...");
progressDialog.show();
// Execute task
MyTask myTask = new MyTask(progressDialog);
AsyncTask<String, Void, ArrayList<HashMap<String, String>>> result =
myTask.execute(userChoice);
Intent intent = new Intent(this, ListActivityA2.class);
// Put results in intent
intent.putExtra("results", resultsList);
// Start result activity
startActivity(intent);
AsyncTask
public class MyTask extends
AsyncTask<String, Void, ArrayList<HashMap<String, String>>> {
ArrayList<HashMap<String, String>> resultsList =
new ArrayList<HashMap<String, String>>();
ProgressDialog progressDialog;
public MyTask(ProgressDialog progressDialog) {
this.progressDialog = progressDialog;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected ArrayList<HashMap<String, String>> doInBackground(
String... params) {
return doProcess(params[0]);
}
@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
progressDialog.dismiss();
super.onPostExecute(result);
}
private ArrayList<HashMap<String, String>> doProcess(String choice) {
// do processing here
return arrayList;
}
}
I have solved this!
I moved the code to create
IntenttoonPostExecuteThe reason behind this: the UI thread (
Activity“A1”) was anyway waiting for theAsyncTaskto return the results so that it could show theListActivity“A2”Now that the creating intent and starting
ListActivity“A2” is happening inonPostExecutethe UI thread can work without freezing.Thanks for all ur help @Aswin Kumar!
Hope this question and answer helps others. 🙂