想要在Activity的onCreate中显示AlertDialog - android

发布于 2024-11-03 01:06:39 字数 1595 浏览 5 评论 0原文

在我的活动中,我在 onCreate() 中调用 MyDialog (自定义对话框)并在 Activity 中处理其 DismissListener 以查找其是否已取消。如果取消,我完成活动,否则加载活动。在此加载期间,我想显示一个警报/进度对话框,让用户知道其正在加载,请稍候。但我看不到该对话框。这就是我的编码方式:

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ld = new AgreeDialog(this);
    ld.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
           if (ld.isCancelled)
    MyActivity.this.finish();
       else {
    //ProgressDialog pd = CreateLoadingDialog();
    //pd.show();
    //Log.i(TAG, "Before Load Is PD showing - " + pd.isShowing());  // Shows true
          /*
    AlertDialog.Builder adb = new AlertDialog.Builder(StartUltimate.this);
    adb.setTitle("Loading...");
    adb.setCancelable(false);
    AlertDialog ad = adb.create();
    ad.show();
    */  
    MyActivity.this.showDialog(0);
    LoadAfteAgree();  // This takes time sonetimes, so want a dialog while this is working 
    MyActivity.this.removeDialog(0);

    //ad.dismiss();
                // pd.dismiss();
    //Log.i(TAG, "After Load Is PD showing - " + ad.isShowing());    // Shows false
     }
}           
    });

@Override
protected Dialog onCreateDialog(int id) {
    switch(id) {
    case 0:
        loadingDlg = new ProgressDialog(this);
        loadingDlg.setMessage("Loading...");
        loadingDlg.setCancelable(false);
        loadingDlg.setIcon(R.drawable.icon);
        return loadingDlg;
    }
    return null;
}

为什么我看不到任何对话框?我也尝试在 LoadAfterAgree() 中调用它们,但也没有成功,结果相同。

非常感谢任何帮助。

谢谢

In my activity, I call a MyDialog (custom dialog) in onCreate() and handle its DismissListener in Activity to find if its cancelled or not. If its cancelled, I finish the activity, else load the activty. During this loading time, I want to show a Alert/Progress dialog to let the user know that its loading, please wait. But am not able to see the dialog. This is how I have coded :

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ld = new AgreeDialog(this);
    ld.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
           if (ld.isCancelled)
    MyActivity.this.finish();
       else {
    //ProgressDialog pd = CreateLoadingDialog();
    //pd.show();
    //Log.i(TAG, "Before Load Is PD showing - " + pd.isShowing());  // Shows true
          /*
    AlertDialog.Builder adb = new AlertDialog.Builder(StartUltimate.this);
    adb.setTitle("Loading...");
    adb.setCancelable(false);
    AlertDialog ad = adb.create();
    ad.show();
    */  
    MyActivity.this.showDialog(0);
    LoadAfteAgree();  // This takes time sonetimes, so want a dialog while this is working 
    MyActivity.this.removeDialog(0);

    //ad.dismiss();
                // pd.dismiss();
    //Log.i(TAG, "After Load Is PD showing - " + ad.isShowing());    // Shows false
     }
}           
    });

@Override
protected Dialog onCreateDialog(int id) {
    switch(id) {
    case 0:
        loadingDlg = new ProgressDialog(this);
        loadingDlg.setMessage("Loading...");
        loadingDlg.setCancelable(false);
        loadingDlg.setIcon(R.drawable.icon);
        return loadingDlg;
    }
    return null;
}

Why am I not able to see any dialog in any way ? I tried calling them in LoadAfterAgree() also, but there also no success, same results.

Any help is highly appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

层林尽染 2024-11-10 01:06:39

您正在 UI 线程中执行长时间操作。将它们移至 AsyncTask 的 doInBackground 方法。请参阅此处的示例。

you're performing your long operations in UI thread. Move them to the AsyncTask's doInBackground method. See the example here.

日裸衫吸 2024-11-10 01:06:39

要停止导致任何内存泄漏的对话框,请确保您的活动中包含以下内容;

AlertDialog _alert;

  @Override
  public void onPause() {
      super.onPause();

      if(_alert != null)
          _alert.dismiss();
  }

To stop your dialog causing any memory leaks, ensure the following is included in your activity;

AlertDialog _alert;

  @Override
  public void onPause() {
      super.onPause();

      if(_alert != null)
          _alert.dismiss();
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文