I have a few Activities on my app that hit a web service. Since I don't want to hold up the main thread, this code is in an AsyncTask. However, I do not want the user to be manipulating the Activity while the web service is being called, so before executing the AsyncTask I show a ProgressDialog which spins and blocks the screen. In the onPostExecute method of the AsyncTask, the first thing I do is dismiss the ProgressDialog.
This should prevent the user from manipulating the Activity without actually blocking the main thread.
However, I've noticed a few times where the ProgressDialog is never dismissed and the user becomes stuck. The AsyncTask has finished, onPostExcute has executed, but the ProgressDialog is still shown. The ProgressDialog has no chance of ever being dismissed and the user is stuck on the screen. Their only option is to visit the Android Settings app and force stop my application.
Does anyone have any idea why this happens? What can I do to fix it?
Relevant code:
This is how I show the ProgressDialog and launch the task:
mProgress = ProgressDialog.show(this, "", "Syncing...", true);
(new MyAsyncTask()).execute(intUserId);
This is the onPostExcute for the task. There is no "#Override"
protected void onPostExecute(Boolean result) {
if (mProgress != null) {
mProgress.dismiss();
mProgress = null;
}
}
Check this:
Make sure you called asynctack.execute() on UI thread.
Use #Override on onPostExcute() to make sure it's properly defined.
Maybe try showing your ProgressDialog in the onPreExecute() Method?
protected void onPreExecute() {
mProgress = ProgressDialog.show(this, "", "Syncing...", true);
}
Give that a shot and see if it works.
Peter Knego posted the following link in a comment under the OP. CommonsWare's solution seems to work well.
Backround task, progress dialog, orientation change - is there any 100% working solution?
Related
In onCreate() I'm showing a ProgressDialog and then calling AsyncTask which is non Activity. In this situation the screen goes black up to 30 seconds. How can I resolve black ui issue?
this from Activity
showdialog("Your call is being connected with available agent...Please wait!");
response = new GetAvailableLink(ConferenceActivity.this).execute()
.get();
Just as I said in a comment, it is from calling .get() on your AsyncTask which is a blocking call so your UI won't proceed and it eliminates the point of the task being asynchronous.
You need to remove .get(). Show your ProgressDialog in onPreExecute() and dismiss it in onPostExecute().
This answer and a linked answer in it should help
I have a sort of numeric keypad and a list. When the user chooses the keypad, I call a function to sort the list. The user can press the button to show the list at any time.
But that list can be pretty large, 500+, so I was thinking about doing the sorting no longer on the UI thread.
What is the best way to do this ?
Should I use a regular thread, asynctask?
My only worry is that the user can also click the button to show the list while the asynctask is not finished yet. How should I handle that ?
Thx
Definitely go for AsyncTask which is designed for heavy work outside UI. Concerning your last question, make button disabled and enable it in AsyncTask's onPostExecute(). Cheers.
Definitely you need a asynctask. In your onCreate method hide all of your buttons and textviews. Before you do something in the background you need to show user a load bar or a spinner so that user can't click any of the buttons you have created. Here is a sample:
class LoadAllProducts extends AsyncTask<String, String, String> {
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Academic.this);
pDialog.setMessage("Loading. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
//Do something
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
//finally show your button
}
});
}
Before using this code make sure you have declared the pDialog. In my program I had used spinner.
AsyncTask looks good for this... task. You can also make some sort of state-member to check whether the sort task is running and depending on it start the task or do nothing when the user presses the button. As an alternative, if something has changed, you can also cancel the task and start a new one.
Go for Async Task and you can show some progress bar in onPreExecute() and stop it in onPostExecute().
User wont be able to clcik anything till progress bar is running on screen.
You can use AsyncTask as other have told or you can create a thread and run long running operations and update UI accordingly with handlers. Have a look at this link http://www.vogella.com/articles/AndroidPerformance/article.html
I hava an activity which might have to load data when it's starting up.
I've read some other questions and they all involved passing the progressdialog to the asycntask (like this one which has to load the data and close it after it's done. this is no option for my application since the method getNewData starts up 5 asynctasks and returns when they're all done.
Anybody has an idea how i can make sure the progressdialog is shown? atm the screen just stays black until the application is done loading.
ProgressDialog pd = ProgressDialog.show(this, "Loading...", "Loading data", true);
dataCommunicator.getNewData(this);
pd.dismiss();
You may have a main AsyncTask inside which you can have the other threads running in doInBackground(). Show the progressDialog in this main AsyncTask onPreExecute().
Maintain an int variable which can be accessed across the child AsyncTask and update its value as the child completes its operations. When that int is equal to 5 return from the main AsyncTask doInBackground() and dismiss the progressDialog in onPostExecute().
I want to make a Progress Dialog Box in my app to use when sending some information. But the code I wrote won't work. It the method send() executes but the dialog box never appears because it dismisses very quickly
Here is my code :
ProgressDialog myProgressDialog = ProgressDialog.show(Tents.this,
"Please wait...", "Sending...", true);
send();
myProgressDialog.dismiss();
goFour();
How do I make the Dialog Box Last a little longer?
First of all - you should not do send() in the same thread as show() and dismiss() - because you are effectively blocking UI thread during sending. The dialog will actually never show - because in order to show it after show() is called, you need to give the control back to the main looper in UI thread and simply finish handling whatever event you are handling. Otherwise the UI thread will never have a chance to draw your dialog.
The best idea is to start running send() in AsyncTask and call dismiss() in onPostExecute() (see http://developer.android.com/reference/android/os/AsyncTask.html to get idea how to run async task).
You are probably getting a progress dialog, but having it immediately dismiss as it has nothing to wait for.
I'll pretend you want this in OnCreate for my example:
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ProgressDialog pd = ProgressDialog.show(this, "Please wait...", "Sending...");
new Thread(new Runnable(){
public void run() {
send();
pd.dismiss();
}
}).start();
gofour();
}
EDIT: If it still goes away immediately, make sure send(); does something that actually takes some time. ;)
The UI thread is used to start send() , this will not work and progress dialog will not be shown .
Call send in another thread or AsynTask doBackground and on completion dismiss the dialog.
If your send action is completing so quickly that the dialog is not displaying properly, might I suggest instead using an indeterminate progress bar in the upper right corner of your activity via requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS) and then utilizing setProgressBarIndeterminateVisibility(true/false).
"I guess my real question would then be how do I make it so that it lasts a little longer?" My answer would be WHY???!!!
I think you would be better showing an alert dialog to confirm your send function has completed, it would be annoying for the user having to wait for no reason!
I'm having a small problem handeling ProgressDialog and the suer hitting the home key.
I create my ProgressDialog as follows:
runOnUiThread(new Runnable() {
public void run() {
progressDialog = ProgressDialog.show(this, "",this.getResources().getString( R.string.AProgressMessage), true);
}
});
and dismiss it when I finished downloading stuff of internet.
progressDialog.dismiss();
the problem is when a user hit the home key, sometimes the Thread that calls the dismiss is kille but hte dialog never gets dismissed... therefore when the app relaunches it gets stuck behind a ProgressDialog.
Any ideas ?
I know this thread is quite old, but I think my answer could help many people.
Activity class has an onPause method. So if you call the dialog from the same activity that is being paused, you can use the onPause method to dimiss the dialog.
Make the dialog variable global, and define the onPause method as follows:
#Override
public void onPause(){
super.onPause();
if(dialog!=null)
dialog.dismiss();
}
Why would the Thread be killed?
If the Android system thinks that the memory is low, it will only kill whole processes and not individual threads. Are you sure you always call progressDialog.dismiss(), even if the thread stops because of an uncaught exception (for example)?
By the way, you should probably use AsyncTask instead of doing the thread management yourself, see here.