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
Related
I use a code that access HTTP service to receive login data, and before it, I tried to show a processing dialog, but it did not showed up. Then I tried to simply change the button label to tell the user to wait until data returns, but it also did not work. That's my code:
tryingToLoginDialog = ProgressDialog.show(this, "Por Favor Aguarde", "Efetuando Login...", true);
btn_Entrar.setText("Wait while logins...");
btn_Entrar.invalidate();
App.webService.Login(txtLogin.getText().toString(), txtSenha.getText().toString());
String LoginUserData = App.webService.getUserData(); /* this method freezes the app but do not crash it cause ThreadPolicy permit all*/
/* here some if's */
tryingToLoginDialog.dismiss();
btn_Entrar.setText("Login");
btn_Entrar.invalidate();
You should use AsynTask. This has 4 different method doInBackground() This method run in background(not in UI Thread) In your case you can do your networking stuff here and other three methods are onProgressUpdate(), onPostExecute() and onPreExecute() run in UI Thread So you can use these function to update GUI.
You were saying you want to show dialog so setup progressdialog in onPreExecute() and do your freezing stuff in doInBackground() and use other method according to your need.
See detail here
I am creating an AlertDialog using an AlertDialog.Builder and showing it. After showing it, I need to pause application until the user comfirms the Dialog.
I exactly need to pause in a method showing Dialog thread, because its calling method throws a fatal error after return.
Is there any way to do that?
In Android, you can't pause the UI-Thread, as it will result in the OS showing the Application is not responding dialog after about 5 seconds of being paused.
Also, as mentioned by CommonsWare in the comments, Dialogs don't operate in a separate thread.
Without seeing your code it's a bit difficult to answer, but what I could suggest right now is place all the code you need to "pause" in an AsyncTask.
You can place all the code before the pause in the onPreExecute() method, than show your dialog, and in the doInBackground() method, maybe in a while loop or something with volatile variables or something (this code runs on a background thread so it won't stuck the UI-Thread) and then the code after the pause in the onPostExecute() method.
both onPreExecute() and onPostExecute() operates on the UI-Thread. the onPost is called after the doInBackground has finished.
But again, if you'd show some code of the pause it would be easier to help you.
Further reading: AsyncTask
Another way to handle this is to use threads. In a non-UI thread, call your AlertDialog's show() method using the runOnUiThread() method of your Activity. Call Object.wait() in your non-UI thread, and call Object.notifyAll() in your AlertDialog's OnClickListener. The non-UI thread will then wait until the user clicks on your AlertDialog.
I'm using an AsyncTask subclass for some background processing. The problem is that when I use the class with the .get() method, the ProgressDialog I specify in the onPreExecute() does not show.
I works fine if I use a callback withing the onPostExecute() method.
My first thought was that this was because the .get() waits for the process to complete but that can't be blocking the UI thread either so that's not the case.
Can anyone explain why this behavior is so and if there is a workaround for this ?? I'd really like to use the .get() method if I can.
I initially accepted the other answer but it seems to be wrong.
The .get() method will block the UI thread to wait for the result and any dialogs displayed will also be blocked. This is the expected behavior for this method.
The only alternative is to not use .get() if the background activity is for any noticable amount of time and instead use callback methods to the calling activity.
Calling AysncTask.get() on UI thread will block UI thread execution and make UI thread waiting for AysncTask.doInBackground() to finish. By doing this, you are actually sacrifice the benefit of AsycnTask, all code now are executed synchronously in UI thread and Background thread (still two thread, but UI thread now wait for background thread).
Also bear in mind that you will probably get ANR exception (blocked more than 5 seconds) by calling get() on UI thread.
If you really have to use it, call your showDialog() method before myAsyncTask.get():
showDialog();
myAsyncTask.execute();
myAsyncTask.get(); // <-- UI thread blocked and wait at this point.
dismissDialog();// <-- This line will be executed after doInBackground() finish.
Hope this helps.
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?
I have an issue with my ProgressDialog object. When I show it first time, it is spinning, but after I dismiss it and show it again, the dialog is static, it it not spinning... Can you please tell me why? All of this happens in the UI thread. Thanks
are you doing any processing in the UI thread during the second show of the ProgressDialog? If so, you need to move the processing to a background thread. Otherwise, the progress dialog does not get the CPU to update(spin) itself.