Android如何判断对话框是否被解除或完成

时间:2014-07-10 11:26:45

标签: android android-activity android-dialog android-dialogfragment

在活动ShowInfoActivity中,用户通过点击按钮显示对话框:

mButtonInfo.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        DialogFragment infoDialog = new UpdateUserInfoDialog();
        infoDialog.show(getFragmentManager(), "Update Info Dialog");
    }
});

我在单独的文件UpdateUserInfoDialog.java中创建了此对话框,并且它有一个空活动UpdateUserInfoActivity.java,这是展示此对话框所必需的。

我有另一种方法,应该在用户完成对话框setPositiveButtonsetNegativeButton时执行。我无法在对话框类中引用ShowInfoActivity

以下是对话框类的代码:

public class UpdateUserInfoDialog extends DialogFragment{

    public static final String TAG = "ChangeWeightDialog";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mReg = Storage.readUserRegistration(getActivity().getApplicationContext());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container
            , Bundle savedInstanceState) {

        if(getDialog() != null) {
            getDialog().setCanceledOnTouchOutside(false);
        }

        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        final LayoutInflater inflater = getActivity().getLayoutInflater();
        final View dialogView = inflater.inflate(R.layout.dialog_update_userinfo, null);

    //Create dialog and stuff......

        builder.setPositiveButton("update", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            //Handle positive click
            }
        });

        builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //handle dnegative
                dismiss();
            }
        });


        builder.setTitle("update personal information");
        builder.setView(dialogView);
        return builder.create();
    }
}

所以我要找的是当用户点击按钮显示活动时要有一个监听器,告诉它是否被解雇或完成:

mButtonInfo.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        DialogFragment infoDialog = new UpdateUserInfoDialog();

        //SOMETHING LIKE:
        infoDialog.whenDismissed(doThisMethod());

        infoDialog.show(getFragmentManager(), "Update Info Dialog");
    }
});

我试过了,但它在OnDissmiss(new ...)行中给出了NullPointer:

mButtonInfo.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        DialogFragment infoDialog = new UpdateUserInfoDialog();
        infoDialog.onDismiss(new DialogInterface() {
            @Override
            public void cancel() {
                showUserInformation();
            }
            @Override
            public void dismiss() {
                showUserInformation();
            }
        });
        infoDialog.show(getFragmentManager(), "Update Info Dialog");
    }
});

1 个答案:

答案 0 :(得分:1)

尝试覆盖UpdateUserInfoDialog中的onDismiss()。它应该广播一个自定义Intent,通知对话框取消。您可以在将调用Intent的特定组件中捕获此doThisMethod()
另请参阅LocalBroadcastManager了解" local"广播。