1) FOA PD (ProgressDialog) can be created only from Activity, doesn't it? Please provide an useful example if it is really not.
2) If a PD should be created in separate thread could it be created and showed if it's thread doesn't do anything at all? I mean something like this (assuming mProgressDialog is a property of the class):
new Thread(){
public void run(){
mProgressDialog = ProgressDialog.show(appContext,
appContext.getResources().getString(R.string.progress_wait),
appContext.getResources().getString(R.string.progress_db_installing),
true);
}.start();
As I understand the thread dies immediately after executing run() cause there is nothing to do and so PD doesn't show. It should have some processing code or at least an empty cycle with some manageable condition
3) if PD should be created in the main thread should it be created only at the end of OnCreate() method or in the body of some method called/caught(by some Listener) started in OnCreate() method?
4) PD by itself doesn't suspend any thread while displaying, does it? So the code continues executing after the show() method . I mean the show() by itself doesn't suspend/pause the thread cause I guessed it does.
1) Not sure how it's relevant unless you can come up with a reason to create a ProgressDialog outside of an Activity context; I think the answer is "no," though.
2) No, you can't create a dialog from a background thread directly. Have you tried your code? It'll die with an exception with a helpful traceback. See any one of a number of SO questions about how to call back to the UI thread to do things like show dialogs.
3) You can create it anywhere in your activity; for example, it's common to do this in onPreExecute() in an AsyncTask, which may be triggered from an onClick callback.
4) No.
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.
Anybody has any idea about ProgressDialog of Andorid with Thread? I am successfully able to display the dialog and dismiss it also, but I have some long logic to be done and then only to dismiss the dialog, until that dialog will display loading message. I put my logic code inside the Run method of thread with While condition that until I didn't get particular result it will run the code. But somehow it is giving Handler and Looper.prepare() error. I try to found this and get to know that I have to use the functionality of Handler class.
ProgressDialog comes under UserInterface.Donot dissmis it inside thread and show before starting thread;
like this.
Thread th=new Thread(this);
ProgressDialog pd=new ProgressDialog(this);
pd.setMessage("Loading...");
pd.show();
th.start();
Now take handler class to dismiss it
void run(){
///do your work;
handler.sendEmptyMessage(0);
}
private Handler handler=new Handler(){
//override onhandle message method and dismiss dialog in it
}
For the same, you can implement AsyncTask concept, and which is very much preferable in Android to implement threading task, by implementing this task, system itself handles the threading task like start, run, stop....etc. and that's why its known as as Painless Threading.
If you have gone through AsyncTask link then i am sure you have noticed those 4 methods of AsyncTask.
For your problem, you can implement the AsyncTask as below:
onPreExecute() - Display progress dialog
doInBackground() - implement all the long running task
onPostExecute() - dismiss progress dialog by calling dialog.dismiss();
I am sure you got it now.
All the best.
Update:
you just need to write Dialog.Show() method inside the onPreExecute() method, and dialog.dismiss() inside the onPostExecute() method. And yes i have told 1000 times that you can't write Toast directly inside the doInBackground() method because Android doesn't allows to ride on the main thread, and if you still want to execute it then write the same Toast message inside the runOnUiThead method (this method should be inside the doInBackground().
I'm having trouble trying to put a ProgressDialog into my app. In my GameEngine class (which doesn't extend anything), I have the code shown below. The first line produces a runtime exception, and although I came across this thread that seems to be about the same error: Android TimerTask throws RuntimeException if Show ProgressDialog is added in run(), I don't really understand how to implement the solution. Any help would be much appreciated, thanks.
//Create ProgressDialog
ProgressDialog dialog = ProgressDialog.show(context, "",
"Loading...", true);
//Set Clusters before level starts
for (int i = 0; i < 80; i++)
{
updateBacteria();
updateAttraction();
checkCollisions();
moveObjectsAwayFromWalls();
}
dialog.dismiss();
You can only show dialogs in the UI thread (which is your main class which extends Activity). To be able to do this, you can write a Handler and use it to send messages from the non UI thread to the UI thread. Android have an example of this in their ProgressDialog example. View the snippet of code they have under "Example ProgressDialog with a second thread".
You can also follow the same method as written in the answer of the link you provided, although a Handler is a more robust approach.
If this method is not running in your Main Activity thread, you should change it. how? Set a Handler in the main activity and pass it to the thread (above). In the handler you should implement the GUI related part of your method (i.e. ProgressDialog). When you need to show the ProgressDialog, just call your Handler and than keep processing (your loop in this case). Same for the dismiss().
I am developing my first Androïd application and I'm facing a problem when I want to display a ProgressDialog to indicate that a process is being run.
In my application, the user triggers a time consuming task by pressing a Button. The "OnClick" function of my "OnClickListener" is called when the user presses the Button. In this function, here is what I'm currently doing :
- creation and configuration of an instance of the ProgressDialog class,
- creation of a thread dedicated to the time consuming task,
- attempt to display the ProgressDialog using the "show" method,
- start of the thread,
- main Activity suspended (call of the "wait" function)
- wake up of the main Activity by the thread when it is finished
- removal of the ProgressDialog by calling the "dismiss" function.
Everything works fine (the result of the long task is correct) but the ProgressDialog nevers appears. What am I doing wrong?
Thanks in advance for the time you will spend trying to help me.
You should not call wait() to the Main Activity/UI thread, because this is actually freezing the UI including your ProgressDialog, so it has no time to fade in and will never be shown.
Try to use multithreading correctly: http://developer.android.com/resources/articles/painless-threading.html
final Handler transThreadHandler = new Handler();
public void onClick(View v) {
// show ProgressDialog...
new Thread(){
public void run(){
// your second thread
doLargeStuffHere();
transThreadHandler.post(new Runnable(){public void run(){
// back in UI thread
// close ProgressDialog...
}});
}
}.start();
}
I would suggest using AsyncTask, as it's purpose is exactly to handle this kind of problem. See here for instructions how to use it. Note that the linked page in Floern's answer also recommends the use of AsyncTask.
You would need to do the following:
subclass AsyncTask
override it's onPreExecute() method to create and show a ProgressDialog. (You could hold a reference to it in a member of your subclass)
override it's doInBackground() method to execute your time consuming action.
override it's onPostExecute() method to hide your dialog.
in your activity, create an instance of your subclass and call execute() on it.
If you make your subclass an inner class of your activity, you can even use all of your activity's members.