即使在 AsyncTask 的 onPreExecute 中,ProgressDialog 也不会显示
在我的类中,Main 扩展了 Activity,我有这个:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case ...
case CREDENTIAL_VIEW:
new SetStatusProgressBar(this).execute();
还有一个嵌套类:
private class SetStatusProgressBar extends AsyncTask<String, Void, Boolean> {
private ProgressDialog dialog;
private Main ctx;
public SetStatusProgressBar(Main ctx) {
this.ctx = ctx;
dialog = new ProgressDialog(ctx);
}
// progress dialog to show user that contacting server.
protected void onPreExecute() {
this.dialog = ProgressDialog.show(ctx, null,
"Refreshing data from server...", true, false);
}
@Override
protected void onPostExecute(final Boolean success) {
//...
//statements that refresh UI
//...
if (dialog.isShowing()) {
dialog.dismiss();
timerProgressBarStop();
}
}
protected Boolean doInBackground(final String... args) {
//...
//statements to download data from server
//...
return true;
}
}
在 Main 类中,我打开第二个 Activity,以这种方式:
Intent myIntent = new Intent(Main.this, Credentials.class);
startActivityForResult(myIntent, CREDENTIAL_VIEW);
第二个 Activity 以这种方式返回到 Main 活动:
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
我不理解为什么当我从第二个 Activity 导航到 Main 时,进度对话框只会在 UI 刷新后显示...这样,进度对话框仅在屏幕上停留半秒...然后隐藏! :( 我想在整个下载过程中看到顶部的进度对话框!
请帮忙。 谢谢大家
In my class, Main extends Activity, I've this:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case ...
case CREDENTIAL_VIEW:
new SetStatusProgressBar(this).execute();
And there is this nested class:
private class SetStatusProgressBar extends AsyncTask<String, Void, Boolean> {
private ProgressDialog dialog;
private Main ctx;
public SetStatusProgressBar(Main ctx) {
this.ctx = ctx;
dialog = new ProgressDialog(ctx);
}
// progress dialog to show user that contacting server.
protected void onPreExecute() {
this.dialog = ProgressDialog.show(ctx, null,
"Refreshing data from server...", true, false);
}
@Override
protected void onPostExecute(final Boolean success) {
//...
//statements that refresh UI
//...
if (dialog.isShowing()) {
dialog.dismiss();
timerProgressBarStop();
}
}
protected Boolean doInBackground(final String... args) {
//...
//statements to download data from server
//...
return true;
}
}
In the Main class I open a second Activity, in this way:
Intent myIntent = new Intent(Main.this, Credentials.class);
startActivityForResult(myIntent, CREDENTIAL_VIEW);
That second Activity returns to the Main activity in this way:
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
I don't understand why when I navigate from the second Activity to the Main, the ProgressDialog will show ONLY AFTER that the UI refreshes... In this way the Progress Dialog stays on the screen only for half second... and then hides! :( I'd like to see the ProgressDialog on top during all the download time!
Help, please.
Thank you all
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,首先使用 getApplicationContext() 而不是 ctx 变量。使用“this”不适合记忆力好的公民。
尝试在 onProgressUpdate(...) 方法中更新 ProgressDialog。
Ok, first of all use getApplicationContext() instead of ctx variable. Using 'this' is not for good memory citizens.
Try updating progressDialog in onProgressUpdate(...) method.