Show ProgressDialog in Fragment class - android

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();
}
}

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);

Related

AsyncTask onPreExecute new progressdialog

class AddStudent extends AsyncTask<String, Void, ResultData> {
private ProgressDialog pDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AddStudentActivity.this);
pDialog.setMessage("Adding Product..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
private Context context;
//CHANGE HERE....ADD PARAMATER
TextView tv_msg;
public AddStudent(Context context, TextView tv_msg) {
this.context = context;
this.tv_msg = tv_msg;
}
I have an error in (AddStudentActivity.this);
Error = com.blablablabla.AddStudentActivity is not an enclosing class.
What's the problem?.
How can I fix this?.
If the asynctask is not a nested class of an activity you need to set/add the context as a parameter to the constructor.
class AddStudent extends AsyncTask<String, Void, ResultData> {
private ProgressDialog pDialog;
private Context context;
public AddStudent(Context context) {
this.context = context;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(context) ;
pDialog.setMessage("Adding Product..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
}
TextView it is part of the activity.
Or, if the asynctask is a nested class of an activity, then you can do what you want. More you can read in the example below:
ProgressDialog and AsyncTask
You have not posted your entire code, so this is a bit of speculation, but here goes:
Most probably you have created a separate file for your AddStudent AsyncTask, or put it outside your AddStudentActivity class. You need to make AddStudent an inner class of AddStudentActivity to be able to use AddStudentActivity.this.
More info here : Android: AsyncTask recommendations: private class or public class?
pDialog = new ProgressDialog(AddStudentActivity.this) ;
change to
pDialog = new ProgressDialog(context) ;
its work.
Error = com.blablablabla.AddStudentActivity is not an enclosing class.
above error comes whenever you try to use Activity context in other separate class .
This should even harmful for you when it will give you memory leaks.
pDialog = new ProgressDialog(context);

how to initialize ProgressDialog in custom adapter

I am trying to use AsuncTask in custom adapter. when i tried to use
class UpdateProductVariantTask extends AsyncTask<String, String, Void> {
private ProgressDialog progressDialog = new ProgressDialog(context);
String result = "";
protected void onPreExecute() {
progressDialog.setCancelable(false);
progressDialog.setMessage("Please wait.....");
progressDialog.show();
progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface arg0) {
UpdateProductVariantTask.this.cancel(true);
}
});
}
}
I am getting this error:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
in line progressDialog.show();
How to fix this error?
try to use Activity not context.
private ProgressDialog progressDialog = new ProgressDialog(activity);

ProgressDialog is not shown in the AsyncTask

Document doc = new Obtainer(context, uri).execute().get();
This code in the activity class renders the Obtainer(which extends AsyncTask) which gets the xml document from the url. This is the onPreExecute method:
protected void onPreExecute() {
super.onPreExecute();
System.out.println("Pre execute began");
exception = null;
dialog = new ProgressDialog(context);
dialog.setMessage("Loading started");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
System.out.println("Preexecute end");
dialog.show();
}
context is set in the Constructor:
public Obtainer(Context c, String addr) {
context = c;
address = addr;
}
During the runtime I can see in the console output both "Pre execute began" and "Preexecute end" but the progress dialog is not shown. What is the probleM?
Use this code, it works for me:
class Obtainer extends AsyncTask<Void, Void, Void> {
private ProgressDialog dialog;
#Override
protected void onPreExecute() {
dialog = new ProgressDialog(App.this); // App - your main activity class
dialog.setMessage("Please, wait...");
dialog.show();
}
#Override
protected Void doInBackground(Void... params) {
// ...
}
#Override
protected void onPostExecute(Void result) {
dialog.dismiss();
}
}
And in your main activity class method call
new Obtainer().execute();
What Context are you passing when you create your Obtainer (AsyncTask subclass)?
If you are using the Application context via getApplicationContext(), it can not be used to create a Dialog (or any View for that matter). You need to pass it a Context that can create Views.
"If you're in the habit of using your application context (from a call to getApplicationContext(), for example) in places where you need a Context to create views, it's only a matter of time until you find a case where things don't work quite like you would want or expect."
From: https://plus.google.com/107708120842840792570/posts/VTeRBsAeyTi

ProgressDialog wont show onPreExecute in my asynctask?

Ive looked at some questions and non answer the problem im having..
I have this asyncTask...
private class LoadData extends AsyncTask<Void, Void, Void>{
protected Void onPreExecute(Void...arg0){
super.onPreExecute();
ProgressDialog dialog = ProgressDialog.show(shoppingClass.this, "",
"Loading. Please wait...", true);
dialog.show();
return null;
}
#Override
protected Void doInBackground(Void... params) {
item = we.getText().toString();
getUserPreference();
itemLookup.loadUrl(url);
return null;
}
#Override
protected void onPostExecute(Void notused){
itemLookup.setVisibility(View.VISIBLE);
}
}
The problem is the progessDialog is not showing up? I dont know why...Im doing everything write according to the documentation.
You are not overriding correctly the method. Change onPreExecute to this:
#Override
protected void onPreExecute() {
super.onPreExecute();
ProgressDialog dialog = ProgressDialog.show(shoppingClass.this, "",
"Loading. Please wait...", true);
dialog.show();
}
it could just be that doinbackground is completing too quickly for you to be able to see the dialog.
Check that shoppingClass.this has the UI context. Also, you shouldn't have to call .show() twice on it and you don't have to return null as its void (lowercase).

How can I dismiss ProgressDialog in AsyncTask

I am using a ProgressDialog to be shown while my background process goes on, but after background process is completed the ProgressDialog is not dismissed still.
Here is my code
private class async extends AsyncTask<String, Void, Boolean> {
final ProgressDialog progressDialog = new ProgressDialog(getParent());
#Override
protected Boolean doInBackground(String... params) {
GetJson json = new GetJson();
boolean success = false;
JSONObject mJsonObject = json
.readJsonObject("url");
try {
success = mJsonObject.getBoolean("success");
} catch (Exception e) {
}
return success;
}
#Override
protected void onPostExecute(Boolean result) {
if (result) {
if (progressDialog.isShowing())
progressDialog.dismiss();
}
}
}
#SuppressWarnings("static-access")
#Override
protected void onPreExecute() {
progressDialog.show(getParent(), "Working..", "Please wait...");
}
}
private final class YourTask extends AsyncTask<Void, Void, Object> {
private ProgressDialog dialog;
#Override
protected void onPreExecute() {
dialog = ProgressDialog.show(YourActivity.this, "Title", "Message", true);
}
#Override
protected Object doInBackground(final Void... params) {
// Doing something
}
#Override
protected void onPostExecute(final Object result) {
// Check result or something
dialog.dismiss();
}
}
You can call progressDialog.dismiss() in your AsyncTask's onPostExecute() method.
In onPostExecute() method call dismiss() on your dialog.
I dont know, if you solved this problem, probably yes.
But for next users what will have the same problem (i had it right now too)..
The problem is in your declaration.
final ProgressDialog progressDialog = new ProgressDialog(getParent());
or in your "second" declaration
progressDialog.show(getParent(), "Working..", "Please wait...");
Because in the first one you put in there the getParent parameter (probably MainActivity.this)
and the same you do in calling show method.
Therefore is there 2 parents.. and if you call dismiss() in post execute..it dismiss the first one..but not the another one what have then dialog.isShowing() equal to false.
So important is have there just 1!!!!! parent content..so you can assign the content in declaration with:
new ProgressDialog(content);
or you can do
ProgressDialog dialog=new ProgressDialog();
dialog.setMessage("Loading");
dialog.show(getParent());
and then dismiss it and everything is allright.
or you can do:
ProgressDialog dialog=new ProgressDialog(getParent());
dialog.setMessage("Loading");
dialog.show();
but never give in there twice parents, contents, whatever..
AsyncTasks should not handle at ALL a dialog. Dismissing the dialog in the PostExecute phase can easily lead to an IllegalStateException as the underlying activity can already be destroyed when the dialog gets dismissed. Before destroying the activity the state of the activity will be saved. Dismissing the dialog afterwards will lead to an inconsistent state.

Categories

Resources