如何检查AlertDialog是否被销毁

时间:2016-01-30 20:17:55

标签: android android-fragments alertdialog

我已经从fragment打开了AlertDialog,之后我关闭了AlertDialog并将数据返回到父片段。

==父片段代码

 @Override
 public void onListItemClick(ListView l, View v, int position, long id) {

    super.onListItemClick(l, v, position, id);

    Bundle arguments = new Bundle();
    arguments.putInt("type", 1100); 

    DialogFragment dialog = AddDialog.newInstance();
    dialog.setArguments(arguments);
    dialog.setTargetFragment(this, CREATE_PAYMENT);
    dialog.show(getFragmentManager(), "dialog");
}

== AlertDialog代码

@Override
 public Dialog onCreateDialog(Bundle savedInstanceState) {
    LayoutInflater inflater = activity.getLayoutInflater();
    final View view = inflater.inflate(R.layout.payment_add, null);

    final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setView(view);

    builder.setView(view).setPositiveButton(R.string.btnSend, null);

    final AlertDialog d = builder.create();
    d.setOnShowListener(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(DialogInterface dialog) {

            Button b = d.getButton(AlertDialog.BUTTON_POSITIVE);
            b.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    // .... do something

                        Intent intent = new Intent();
                        intent.putExtra("payment", operation);
                        getTargetFragment().onActivityResult(getTargetRequestCode(),
                                Activity.RESULT_OK, intent);
                        d.dismiss();
                    }
                }
            });
        }
    });

    return d;
 }

按下确定后我已经返回到父片段,完成了一些操作并将父片段替换为另一个片段。   但不幸的是,在更换片段时,会出现新片段的颤抖,因为我认为在Dismiss()之后,AlertDialog并没有从主Activity中销毁;

我是这么认为的,因为我没有在程序的其他部分使用相同的片段发抖,因为没有AlertDialog。

==父片段代码

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        switch (requestCode) {

            // ... do something

           Sign newFragment = Sign.newInstance(new_operation);
           FragmentManager fragmentManager =   getActivity().getSupportFragmentManager();
           fragmentManager.beginTransaction()
                .replace(R.id.main, newFragment)
                .addToBackStack(null)
                .commit();

           break;
        }
    }
}

如何在关闭和销毁AlertDialog后立即替换片段。

感谢名单。

1 个答案:

答案 0 :(得分:1)

在对话框中添加此内容

d.setOnCancelListener(new OnCancelListener() {
    @Override
    public void onCancel(DialogInterface dialog) {
        // Replace your fragment             
    }
});

d.setOnDismissListener(new OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialogInterface) {
        // Replace your fragment
    }
});

一旦取消/取消对话框,就会调用上述回调方法。