单击 HOME 并返回应用程序后不显示 ProgressDialog

发布于 2024-12-28 17:12:20 字数 3228 浏览 0 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉栀 2025-01-04 17:12:20

您没有重新启动应用程序吗?如果异步任务仍在进行,我假设您的应用程序刚刚暂停,并将执行 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.

红焚 2025-01-04 17:12:20

添加这些行

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

并将其添加到您的清单活动中

 android:configChanges="keyboardHidden|orientation|navigation"

Add these lines

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

and add this line in your manifest activity

 android:configChanges="keyboardHidden|orientation|navigation"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文