30

I am trying to show a ProgressDialog within a Fragment class. The following code just works within an Activity class but not for Fragment. Can somebody please help me on this, why this ProgressDialog implementaion just works within an Activity and not for a Fragment?

private class ProcessUpdateProfile extends
        AsyncTask<String, String, JSONObject> {

    private ProgressDialog nDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        nDialog = new ProgressDialog(PFragment.this); //Here I get an error: The constructor ProgressDialog(PFragment) is undefined
        nDialog.setMessage("Loading..");
        nDialog.setTitle("Checking Network");
        nDialog.setIndeterminate(false);
        nDialog.setCancelable(true);
        nDialog.show();

    }
}
Seraphim's
  • 12,559
  • 20
  • 88
  • 129
Sini Inis
  • 417
  • 1
  • 7
  • 16

2 Answers2

68

Try this in Fragment

 nDialog = new ProgressDialog(getActivity()); 
M D
  • 47,665
  • 9
  • 93
  • 114
15

ProgressDialog take Context input so use getActivity() in object creation.

ProgressDialog dialog = ProgressDialog.show(getActivity(), "Loading...", "Please wait...", true);
Menma
  • 799
  • 1
  • 9
  • 35
navneet sharma
  • 680
  • 4
  • 7