Dialog Fragment无法将事件传递回Android中调用Fragment?

时间:2019-06-16 20:35:19

标签: android android-fragments kotlin android-dialogfragment

在我的Android应用中,我的导航抽屉活动为MainActivity。 而且,除了导航之外,我无意在MainActivity中执行任何其他任务,我想保持简单并使用Fragments进行任务。

所以我有一个Fragment,它调用DialogFragmentDialogFragment将事件传递回Fragment。我的实现类似于docs中此处给出的代码。

但是,当需要显示对话框时,我的应用程序崩溃了。

这是我的DialogFragment

class DialogSubjectsProgress: DialogFragment() {
    internal lateinit var listener: TheDialogListener

    interface TheDialogListener {
        fun onDialogCancel(dialog: DialogFragment)
        fun onDialogDismiss(dialog: DialogFragment)
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        listener = context as TheDialogListener
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        super.onCreateDialog(savedInstanceState)

        // show progress dialog
        val progressView = activity!!.layoutInflater.inflate(R.layout.dialog_progress_i, null)
        progressView.findViewById<TextView>(R.id.progress_message).text = "Fetching data"

        return activity!!.let {
            val progressBuilder = AlertDialog.Builder(it, R.style.ThemeOverlay_AppCompat_Dialog)
            progressBuilder
                .setView(progressView)
                .setOnCancelListener {
                    listener.onDialogCancel(this)
                }
                .setOnDismissListener {
                    listener.onDialogDismiss(this)
                }
                .create()
        }
    }
}

这是我的Fragment代码,根据文档,该代码用于Activity

class FragmentSubjects: Fragment(), DialogSubjectsProgress.TheDialogListener {

    // other code

    private fun addSubject() {
        // show progress dialog
        val progressDialog = DialogSubjectsProgress()
        progressDialog.show(activity!!.supportFragmentManager, "null")

        // todo
        // progressDialog.dismiss()

    }

    override fun onDialogCancel(dialog: DialogFragment) {
        // User canceled the dialog
    }

    override fun onDialogDismiss(dialog: DialogFragment) {
        // Dialog's work is done
        // todo
    }

}

我已经找到了对此here的解决方案,但是由于它是用Java编写的,所以我并不真正理解它,而且我也不了解Java。我需要Kotlin方面的帮助!

----编辑----

这是log

java.lang.ClassCastException: com.example.kotlinappv3.MainActivity cannot be cast to com.example.kotlinappv3.DialogSubjectsProgress$TheDialogListener
        at com.example.kotlinappv3.DialogSubjectsProgress.onAttach(DialogSubjectsProgress.kt:27)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1404)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
        at androidx.fragment.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
        at android.os.Handler.handleCallback(Handler.java:874)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:198)
        at android.app.ActivityThread.main(ActivityThread.java:6729)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

at com.example.kotlinappv3.DialogSubjectsProgress.onAttach(DialogSubjectsProgress.kt:27)

指向

listener = context as TheDialogListener

使用我的DialogFragment代码。

1 个答案:

答案 0 :(得分:1)

在您的addSubject方法中

而不是

activity!!.supportFragmentManager

使用:

childFragmentManager

并在DialogFragment代码中进行更改:

override fun onAttach(context: Context) {
    super.onAttach(context)
    listener = context as TheDialogListener
}

收件人:

override fun onAttachFragment(childFragment: Fragment?) {
    super.onAttachFragment(childFragment)
    listener = context as TheDialogListener
}