Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show ProgressDialog in Fragment class

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();      } } 
like image 986
Sini Inis Avatar asked Jul 18 '14 12:07

Sini Inis


People also ask

How do you know if a fragment is destroyed?

Since all fragments are destroyed if the activity is destroyed, a simple answer could be calling getActivity(). isDestroyed() returning true if the activity is destroyed, therefore the fragment is destroyed.

Can fragments be used in multiple activities?

You can use multiple instances of the same fragment class within the same activity, in multiple activities, or even as a child of another fragment. With this in mind, you should only provide a fragment with the logic necessary to manage its own UI.

How do I find a fragment in an activity?

To get the current fragment that's active in your Android Activity class, you need to use the supportFragmentManager object. The supportFragmentManager has findFragmentById() and findFragmentByTag() methods that you can use to get a fragment instance.

What is ID of fragment in Android Studio?

public abstract Fragment findFragmentById (int id) Finds a fragment that was identified by the given id either when inflated from XML or as the container ID when added in a transaction. The important part is "as the container ID when added in a transaction".


2 Answers

Try this in Fragment

 nDialog = new ProgressDialog(getActivity());  
like image 138
M D Avatar answered Sep 23 '22 08:09

M D


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

ProgressDialog dialog = ProgressDialog.show(getActivity(), "Loading...", "Please wait...", true); 
like image 31
navneet sharma Avatar answered Sep 21 '22 08:09

navneet sharma