单击HOME并返回到App后,ProgressDialog不会显示

时间:2012-01-24 15:38:32

标签: android dialog 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完成后,对话框才会消失。如果我单击home然后返回到应用程序,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();
}

} `

2 个答案:

答案 0 :(得分:0)

你没有重新启动应用程序,不是吗?如果异步任务仍然存在,我假设您的应用程序刚刚暂停,并将通过onResume而不是。 onResume应该显示它,onPause应该关闭它,如果没有创建任务栏通知器。

答案 1 :(得分:0)

添加这些行

@Override
public void onResume()
{
    super.onResume();
    if(dialog!=null)
    dialog.show();
}

并在清单活动中添加此行

 android:configChanges="keyboardHidden|orientation|navigation"