无法关闭我的自定义对话框

时间:2012-08-14 13:03:06

标签: android dialog

对话框:

public class ClearDialog extends Dialog {

    private MainActivity context;

    public ClearDialog(MainActivity context) {
        super(context);
        this.context = context;
        setContentView(R.layout.clear_dialog);
        setTitle("something");
        setCanceledOnTouchOutside(false);
        setCancelable(true);
    }
    /* not overriding anymore
    @Override
    public void onBackPressed() {
        return;
    }
    still doesnt work */

    @Override
    protected void onStart() {
        super.onStart();
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = preferences.edit();
        editor.clear();
        editor.commit();
        ResourceHelpers.removeAllResources();
        context.onResourcesDeleted();
    }

}

活动:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.itemLogoff:
            loginDialog.show(); //this is another dialog
            break;
        case R.id.itemSync:
            Intent syncer = new Intent(MainActivity.this, SyncActivity.class);
            MainActivity.this.startActivity(syncer);
            break;
        case R.id.itemClear:
            new AlertDialog.Builder(this)
            .setIcon(R.drawable.ic_action_alert)
            .setTitle("something")
            .setMessage("something")
            .setPositiveButton("something", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    showDeleteDialog();
                }
            })
            .setNegativeButton("something", null)
            .show();
            break;
        }
        return true;
    }

    private void showDeleteDialog() {
        cd = new ClearDialog(this); //this is the dialog
        cd.show();
    }

    public void onResourcesDeleted() {

        cd.dismiss();
        loginDialog.show();
    }

所以..用户点击ActionBar中的“删除所有数据”(optionsmenu)。我打开一个AlertDialog询问他是否确定。然后,如果他确定,我打开一个显示旋转ProgressBar的对话框。

问题:它不会被解雇!

loginDialog(所有数据都丢失,所以我希望用户再次登录......)出现在后台。 ClearDialog不会解雇......

3 个答案:

答案 0 :(得分:1)

我认为问题在这里(不要以这种方式覆盖该方法):

@Override
    public void onBackPressed() {
        return;
    }

您已经可以使用.setCancelable(false)

获取模式对话框

请参阅此文档:http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog

答案 1 :(得分:0)

提供以下属性进行对话     .setCancelable(真);

就像代码中的.setTitle()或.setMessage一样......

答案 2 :(得分:0)

除了您应该遵循的StErMi的答案之外,还要在onResourcesDeleted()方法中切换两行。调用登录对话框,并在调用dismiss之前接管。

public void onResourcesDeleted() {
    cd.dismiss();
    loginDialog.show();
}