单击 HOME 并返回应用程序后不显示 ProgressDialog
在我的 onCreate 中,执行以下代码:
dialog = new ProgressDialog(this);
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.setMessage("Welcome! Initializing database... This will take just a minute");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMax(100);
dialog.setProgress(0);
dialog.show();
该对话框仅应在 AsyncTask 完成后消失。如果我单击主页,然后返回到应用程序,ProgressDialog 应该仍然在那里,事实上,这就是模拟器中发生的情况。但是,在我的 HTC Evo 上,如果我单击主页并返回到应用程序,则不会显示该对话框。
我还有:
@Override
protected void onRestart() {
super.onRestart();
try {
dialog.show();
}
catch(Exception e) {}
}
关于可能导致这种情况的原因有什么想法吗?
更新(最相关的代码):
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
RecipeDbAdapter rdAdapter;
int recipesInDb = 0;
// Binds automatically to "@android:id/list"
setContentView(R.layout.country_list);
setListAdapter(new EfficientAdapter(this));
handler = new Handler() {
public void handleMessage(Message msg) {
Bundle b = msg.getData();
String type = b.getString("type");
String data = b.getString("data");
// If message received to advance dialog (called during initial db load)
try {
if (type.equals("dialog")) {
dialog.incrementProgressBy(Integer.parseInt(data));
if (dialog.getProgress() >= dialog.getMax()) {
dialog.setProgress(dialog.getMax());
currentlyLoadingFromDb = false;
dialog.dismiss();
dialog = null;
}
} else if (type.equals("update")) {
updateLastRecipeUpdateDate();
}
} catch (Exception e) {
}
}
};
try {
rdAdapter = new RecipeDbAdapter(Countries.this);
rdAdapter.open();
recipesInDb = rdAdapter.fetchAllRecipesCount();
rdAdapter.close();
// Initialize database of recipes
if (recipesInDb == 0) {
currentlyLoadingFromDb = true;
dialog = new ProgressDialog(this);
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.setMessage("Welcome! Initializing database of recipes... This will take just a minute");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMax(100);
dialog.setProgress(0);
dialog.show();
updateLastRecipeUpdateDate();
loadAT = new LoadDatabaseTask().execute("");
}
rdAdapter.close();
} catch (Exception e) {
}
}
@Override
protected void onPause() {
try {
dialog.dismiss();
}
catch(Exception e) {}
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
if (dialog!=null)
try {
dialog.show();
}
catch(Exception e) {}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
} `
In my onCreate, the following code executes:
dialog = new ProgressDialog(this);
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.setMessage("Welcome! Initializing database... This will take just a minute");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMax(100);
dialog.setProgress(0);
dialog.show();
The dialog should only go away once an AsyncTask completes. If I click home and then return to the application the ProgressDialog should still be there, and in fact, that's what happens in the emulator. On my HTC Evo, however, the dialog does not show if I click home and return to the app.
I also have:
@Override
protected void onRestart() {
super.onRestart();
try {
dialog.show();
}
catch(Exception e) {}
}
Any ideas as to what could be causing this?
Updated (most relevant code):
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
RecipeDbAdapter rdAdapter;
int recipesInDb = 0;
// Binds automatically to "@android:id/list"
setContentView(R.layout.country_list);
setListAdapter(new EfficientAdapter(this));
handler = new Handler() {
public void handleMessage(Message msg) {
Bundle b = msg.getData();
String type = b.getString("type");
String data = b.getString("data");
// If message received to advance dialog (called during initial db load)
try {
if (type.equals("dialog")) {
dialog.incrementProgressBy(Integer.parseInt(data));
if (dialog.getProgress() >= dialog.getMax()) {
dialog.setProgress(dialog.getMax());
currentlyLoadingFromDb = false;
dialog.dismiss();
dialog = null;
}
} else if (type.equals("update")) {
updateLastRecipeUpdateDate();
}
} catch (Exception e) {
}
}
};
try {
rdAdapter = new RecipeDbAdapter(Countries.this);
rdAdapter.open();
recipesInDb = rdAdapter.fetchAllRecipesCount();
rdAdapter.close();
// Initialize database of recipes
if (recipesInDb == 0) {
currentlyLoadingFromDb = true;
dialog = new ProgressDialog(this);
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.setMessage("Welcome! Initializing database of recipes... This will take just a minute");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMax(100);
dialog.setProgress(0);
dialog.show();
updateLastRecipeUpdateDate();
loadAT = new LoadDatabaseTask().execute("");
}
rdAdapter.close();
} catch (Exception e) {
}
}
@Override
protected void onPause() {
try {
dialog.dismiss();
}
catch(Exception e) {}
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
if (dialog!=null)
try {
dialog.show();
}
catch(Exception e) {}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有重新启动应用程序吗?如果异步任务仍在进行,我假设您的应用程序刚刚暂停,并将执行 onResume 。 onResume 应该显示它,onPause 应该关闭它,如果没有创建任务栏通知程序。
your not restarting the application are you? if the async task is still going i assume that your app was just paused and will go through onResume instead. onResume should show it and onPause should dismiss it, if not create a taskbar notifier.
添加这些行
并将其添加到您的清单活动中
Add these lines
and add this line in your manifest activity