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(); } }
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.
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.
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.
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".
Try this in Fragment
nDialog = new ProgressDialog(getActivity());
ProgressDialog
take Context
input so use getActivity()
in object creation.
ProgressDialog dialog = ProgressDialog.show(getActivity(), "Loading...", "Please wait...", true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With