So, my progress dialog not dismiss in onPostExecute. I'm using fragment. I didn't know where my problem is. I had search and try many solution, but all of that not working
As I said before, I had try many solution but it didn't work. And my progress dialog still show.
This is how I run the AsyncTask
new FetchDataServer().execute();
And here is my AsyncTask class code. Tell me where my mistake is.
private class FetchDataServer extends AsyncTask<String,String,String>{
#Override
protected void onPreExecute() {
super.onPreExecute();
pd = true;
progressDialog.show(getActivity(), "Permintaan diproses", "Loading...");
}
#Override
protected String doInBackground(String... strings) {
getOneWayResult(input_data);
progressDialog.dismiss();
return null;
}
#Override
protected void onPostExecute(String s) {
Log.d("HAHA","Executed");
System.out.println("Masuk post execute");
if(progressDialog.isShowing() || pd){
progressDialog.dismiss();
pd = false;
}
}
}
I expected it to be dismiss immediately. It really disturbing my job. Currently in hurry to collect it. Thanks for your time.
Related
During android app onCreate I have a bunch of code that can be slow on some older devices. I want to have a progressdialog display while all the init code runs. Dialog won't show if called from onCreate because further code blocks the UI. So it needs to go into an AsyncTask.
This is my task.
class startupTask extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
pd = new ProgressDialog(mainContext);
pd.setMessage("Starting up. Please wait...");
pd.setIndeterminate(true);
pd.setCancelable(false);
pd.show();
super.onPreExecute();
}
#Override
protected Void doInBackground(Void... params) {
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
pd.show();
runInit();
}
});
return null;
}
#Override
protected void onPostExecute(Void result) {
if (pd != null) {
pd.dismiss();
}
super.onPostExecute(result);
}
}
But the dialog still does not show? runInit is the usual slow startup code. It needs to run on the UI.
If I remove the pd.dismiss call the progress dialog does appear once the init codeis finished, so it is being created OK, just not displaying.
According to other posts the above code is the right way to do it, but when called from onCreate it does not seem to work?
Any ways to force a dialog to display after a call to show? Any way I can get the dialog to show and wait until it is visible before continuing?
Thanks for any help.
EDIT: In the end I just had to bite the bullet and reorganise code so that only the slow non UI code was inside the AsyncTask. This got it working.
What's your runInit is doing? You're running on the UI thread, it's being blocked the same way.
onPreExecute(), onPostExecute() and onProgressUpdate() have access to the UI Thread, but doInBackground() doesn't.
if runInit() does something on the UI it's being blocked and may be generating an exception that you're ignoring.
you should do operations on doInBackground and use publishProgress() to send to onProgressUpdate() data that you want to update to your UI / ProgressBar Status.
here's an example:
new AsyncTask<Void,Object,Void>()
{
#Override
protected Void doInBackground(Void... params)
{
Object[] _progress = new Object[]{"Decompressing", 1};
publishProgress(_progress);
}
#Override
protected void onPostExecute(Void params)
{
}
#Override
protected void onPreExecute()
{
}
#Override
protected void onProgressUpdate(Object... values)
{
txtProgress.setText(values[0].toString());
progressBar.setProgress(Integer.parseInt(values[1].toString()));
}
}.execute();
I need to show simple progress bar dialog while some method not finish.
I Try to call it like
ProgressDialog progressDialog = ProgressDialog.show(Activity.this, "", "Please wait");
SyncCity()
SyncStreet()
progressDialog.dismiss();
But than app is blocked while method not finish,after that i get progress dialog,and in next second dissapear, sometimes i not See it at all.
All calling is going on button click...
Where is catch?
Thank You.
That's because you're blocking the UI thread. Try using AsyncTask, like this:
ProgressDialog progressDialog;
new AsyncTask<Void, Void, Void>() {
#Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(Activity.this, "", "Please wait");
}
#Override
protected Void doInBackground(Void... params) {
SyncCity()
SyncStreet()
return null;
}
#Override
protected void onPostExecute(Void args) {
progressDialog.dismiss();
}
}.execute();
You should use an AsyncTask to do such processes. You shouldn't do any process in the UI thread.
Here is a good example on how to use AsyncTask:
http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html
I am new in android. I am trying to display ProgressDialog when click on button .
This is my code:
// set listener
btn_Login.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//progress.show();
MyAsynch asynch = new MyAsynch();
asynch.execute();
}
In this code progress dialog too much late appear when i am comment on Asynctask object then progress dialog appear normally.
I am puting my progress dialog in
AsynchTask method
onPreExecute() but same out put dialog display late .
How to solve my problem..??
I am also read stack answers following link but not solve my problem .
async task progress dialog show too late
ProgressDialog appears too late and dissapears too fast
here is my Asynctask code
private class MyAsynch extends AsyncTask<String, Void, String> {
ProgressDialog progress;
String login_stat;
String stat;
#Override
protected void onPreExecute() {
progress = new ProgressDialog(this);
progress.setTitle(" User Login ");
progress.setMessage("Please Wait!!");
progress.setCancelable(false);
progress.setIndeterminate(true);
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.show();
}
#Override
protected String doInBackground(String... urls) {
try {
login_stat = s_ApiHandling.doLogin(m_Et_Username.getText()
.toString().trim(), m_Et_Password.getText()
.toString().trim());
} catch (Exception e) {
System.out.println("internet connection loss ");
stat = "ERORR";
e.printStackTrace();
}
return stat;
}
#Override
protected void onPostExecute(String status) {
progress.dismiss();
}
}
You are probably doing too much in onPreExecute
Remove progress.cancel() from your doInBackground method and put it in to a onPostExecute method in your AsyncTask (like the second link you posted)
You shouldn't have anything talking to the UI in a background thread - that should all be done in pre/post execution.
you code should look like this:
AsyncTask<String, Void, String>()
{
private ProgressDialog progressDialog = ProgressDialog.show(this, "", "Loading...");
#Override
protected void onPostExecute(String result)
{
progressDialog.dismiss();
}
#Override
protected String[] doInBackground(String... params)
{
//ALL CODE GOES HERE.
}
}
When you call the asynctask you must not use the get() method or the progress dialog won't work correctly.
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).
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.