如何在警报对话框和片段之间进行通信?

时间:2019-05-23 15:10:48

标签: java android kotlin

我有一个名为AlertDialogFragment的类,该类将在整个程序需要时用于创建警报对话框。如果单击肯定按钮,则创建对话框的片段将调用一个函数;如果单击否定按钮,则将不执行任何操作。我是android开发的新手,任何提示或帮助都值得赞赏。

这是我的AlertDialogFragment:

class AlertDialogFragment : DialogFragment() {


    companion object {
        private val TAG = "AlertDialogFragment"


        fun newInstance(message: String, positiveBtnText: String, negativeBtnText: String): AlertDialogFragment {
            val fragment = AlertDialogFragment()
            fragment.isCancelable = false

            val args = Bundle()
            args.putString("aMessage", message)
            args.putString("aPositiveBtnText", positiveBtnText)
            args.putString("aNegativeBtnText", negativeBtnText)

            fragment.arguments = args
            return fragment
        }


    }


    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        Log.d(TAG, "onCreateDialog called")
        super.onCreateDialog(savedInstanceState)
        return activity?.let {
            val builder = AlertDialog.Builder(it)
            builder.setMessage(arguments?.getString("aMessage"))
                    .setPositiveButton(arguments?.getString("aPositiveBtnText"), DialogInterface.OnClickListener { dialog, id ->
                        Log.d(TAG, "Yes!")
                    })
                    .setNegativeButton(arguments?.getString("aNegativeBtnText"), DialogInterface.OnClickListener { dialog, id ->
                        Log.d(TAG, "Dismiss!")
                    })

            Log.d(TAG, "onCreateDialog ending")
            builder.create()
        } ?: throw IllegalStateException("Activity can not be null")
    }


} 

这是我尝试初始化警报对话框并设置目标片段的方法:

try {
            AlertDialogFragment alertDialogFragment = new AlertDialogFragment().Companion.newInstance(
                    "Would you like to continue?",
                    "Yes",
                    "Dismiss");
            alertDialogFragment.setTargetFragment(this, TARGET_FRAGMENT_REQUEST_CODE);
            alertDialogFragment.show(getActivity().getSupportFragmentManager(), "dialog");
        }catch (Exception e){
            e.printStackTrace();
        }

在此先感谢您的帮助!

编辑:我在警报对话框中使用kotlin,初始化它的片段在Java中。

1 个答案:

答案 0 :(得分:1)

您可以检索目标片段并请求传入的代码,然后使用该代码来调用onActivityResult

targetFragment?.let { fragment ->
    fragment.onActivityResult(fragment.targetRequestCode, Activity.RESULT_OK, null)
}

并在您的函数中:

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        Log.d(TAG, "onCreateDialog called")
        super.onCreateDialog(savedInstanceState)
        return activity?.let {
            val builder = AlertDialog.Builder(it)
            builder.setMessage(arguments?.getString("aMessage"))
                    .setPositiveButton(arguments?.getString("aPositiveBtnText"), DialogInterface.OnClickListener { dialog, id ->
                        Log.d(TAG, "Yes!")
                        targetFragment?.let { fragment ->
                            fragment.onActivityResult(fragment.targetRequestCode, Activity.RESULT_OK, null)
                        }
                    })
                    .setNegativeButton(arguments?.getString("aNegativeBtnText"), DialogInterface.OnClickListener { dialog, id ->
                        Log.d(TAG, "Dismiss!")
                    })

            Log.d(TAG, "onCreateDialog ending")
            builder.create()
        } ?: throw IllegalStateException("Activity can not be null")
    }

在启动片段中,您将覆盖onActivityResult并处理返回的值。

请注意,如果重新创建了片段,则必须重新分配目标片段。