My ProgressDialog works in the case that i show() in my oncreate method.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pd = ProgressDialog.show(this, "Working", "Retreiving Neaby Restaurants", true, true); // initializes the progress dialog
pd.setCanceledOnTouchOutside(false); // on newer versions of android touching outside of the screen will close the dialog. I decided to make it only cancelable when the back button is pressed.
setContentView(R.layout.nearby_places_list);
However, the dialog code wont work if i use it in an async task because its not in the on create(). The arguements show(this, “Working”, “Retreiving Neaby Restaurants”, true, true) arnt allowed outside of the onCreate():
class MyAsync extends AsyncTask<Void, Integer, Boolean>{ // this task is for populating the listview
@Override
protected void onPreExecute() {
//ProgressDialog.show(context, "Working", "Retreiving Neaby Restaurants");
pd = ProgressDialog.show(this, "Working", "Retreiving Neaby Restaurants", true, true); // initializes the progress dialog
pd.setCanceledOnTouchOutside(false); // on newer versions of android touching outside of the screen will close the dialog. I decided to make it only cancelable when the back button is pressed.
}
This is somewhat upsetting because i wanted to show the dialog at the start of an async task but not long before that task is called.
So my question is how do make this work inside of the async task on the onPreExecute() I don’t want to have to show() until the users actually start using the async task.
use
in
onPreExecute()this is actually the
Contextof the activity. An important thing you need to use often in future.You can also make a
Class level variableContext activityContextand initialize it after
setContentViewinonCreatelike thisactivityContext=thisand then you can also use
pd = ProgressDialog.show(activityContext, "Working", "Retreiving Neaby Restaurants", true, true);