【问题标题】:ProgressDialog should not be cancelableProgressDialog 不应该是可取消的
【发布时间】:2016-06-21 08:29:47
【问题描述】:

我做了一些需要几秒钟的数据库保存。同时,我想显示我的 ProgressDialog 以表明该应用正在运行它。

    class CreateNewSaves extends AsyncTask<String, String, String> {
        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(activityContext);
            pDialog.setMessage(activityContext.getString(R.string.upload_message));
            pDialog.setIndeterminate(false);
            pDialog.show();
            pDialog.setCancelable(false);
            pDialog.setCanceledOnTouchOutside(false);
        }
        /**
         * Creating saves
         * */
        protected String doInBackground(String... args) {
            //Some work done
            return null;
        }
        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once done
            pDialog.dismiss();
        }

我不希望此 ProgressDialog 可取消,因此我愿意

    setCancelable(false);
    setCanceledOnTouchOutside(false);

但是当触摸屏幕或返回按钮时,ProgressDialog 仍然被取消。 我不确定它是隐藏还是关闭。

【问题讨论】:

    标签: android progressdialog


    【解决方案1】:
            pDialog.show(); // this line should be at last. show will create your dialog, rest values will have no impact.
    
            pDialog.setCancelable(false);
            pDialog.setCanceledOnTouchOutside(false);
    

    替换为

                pDialog.setCancelable(false);
                pDialog.setCanceledOnTouchOutside(false);     
                pDialog.show();
    

    【讨论】:

    • 这是我第一次尝试的方式。但是,将其恢复为这种方式,然后进行项目重建工作。有时必须重建项目以应用更改是否正常? (我猜这个项目重建有助于考虑 setCancelable 和 setCanceledOnTouchOutside 的变化)
    • 如果android不需要rebuild,运行项目。它应该反映在应用程序中。否则杀死应用程序再次运行,最后的选择肯定会卸载并安装应用程序。
    • 当我停止调试并再次以调试模式启动应用程序时,有时会发生更改未应用的情况。我可以看到,当应用程序在空行的断点处停止时 - 在更改之前有一些东西。
    猜你喜欢
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-26
    • 2011-05-19
    相关资源
    最近更新 更多