I am new to the android.os.AsyncTask approach on handling android.app.ProgressDialog. I have this problem of processing a particular query via android.database.Cursor and convert in into an entity I would need.
I am following this template but it always return me an android.view.WindowManager$BadTokenException.
List<Entity> loadData() throws Exception {
AsyncTask<Cursor, Void, List<Entity>> process = new AsyncTask<Cursor, Void, List<Entity>>() {
ProgressDialog dialog;
protected void onPreExecute() {
dialog = ProgressDialog.show(getApplicationContext(), "Please wait...", "Loading data...");
}
protected void onPostExcecute() {
dialog.dismiss();
}
@Override
protected List<Entity> doInBackground(Cursor... params) {
List<Entity> entities = new ArrayList<Entity>();
// process of convertion of data from android.database.Cursor to <pacakge>.Entity
return entities;
}
}.execute(/* the query : android.database.Cursor */)
return process.get();
}
Am I missing something?
to show
ProgressDialogfromonPreExecute()method of AsyncTask use Current Activity Context in which AsyncTask is running instead ofgetApplicationContext()as :if
AsyncTaskis running in separate class fromActivitythen you will need to pass current activity context in AsyncTask using AsyncTask’s class constructor