Reputation: 2613
my app is loading some data from a web server.this time i would like to have a progress dialog in order to avoid a black screen to the app users.
Button ok = (Button) findViewById(R.id.ok);
ok.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
MyDialog = ProgressDialog.show( yassou.this, " " , " Loading. Please wait ... ", true);
MyDialog.show();
// myIntent.username.getText();
try {
Send();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Status();
//MyDialog=null;
}
});
this is the btn witch "onclick" is loading the data.Send()
; is a method that sends some data that the user enters to my server,andStatus();
is a second method that directs me to a new page.unfortunately,as i press the ok button,the app first goes to the second page and secondly appears a non stop progress dialog.Where is my wrong please?
Upvotes: 1
Views: 1179
Reputation: 1404
try this
Button ok = (Button) findViewById(R.id.ok);
ok.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
MyDialog = ProgressDialog.show( yassou.this, " " , " Loading. Please wait ... ", true);
MyDialog.show();
// myIntent.username.getText();
try {
Send();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
MyDialog.dismiss();
Status();
//MyDialog=null;
}
});
Upvotes: 0
Reputation: 15267
You have to dismiss dialogs when you don't need them anymore. It appears after you are sent to the "second page" because all the onClick()
code is executed before changes to the UI happen.
It's important that you do all the Send()
and Status()
work in an AsyncTask
or in a thread (which, at the end, will dismiss the dialog), because while that code is being executed the UI will be locked, and won't be able to show the dialog. Moreover, if the job is too long you could get an Application Not Responding (ANR) message.
A good rule is that any method executed in UI thread should last less than 200ms, to guarantee responsiveness and not to result sluggish to the user.
Creating Dialogs (from Android Dev)
Upvotes: 1