1

I have been doing communication between a DialogFragment and Fragment completely wrong, through an activity that implements the interface, then tells the fragment what to do. This just seems like a waste of space and full of potential errors, so I've been working on doing the communication directly to the fragment. I have been trying to figure out the best way to do this. I have found 3 ways, but I'm not sure which would be best to implement, most particularly when I only have 1 function I want to use from the DialogFragment.

Option 1: Interface

This seems like the "Go To" method, my fragment implements the listener, and then in the DialogFragment I put

override fun onAttach(context: Context) {
    try {
        dialogListener = parentFragment as MyDialogListener
    }catch (exception: ClassCastException){
        throw ClassCastException("$parentFragment must implement MyDialogListener")
    }
}

This is how I've been doing it (except before I was using context, now directly to the fragment) but I'm not sure if it's worth creating an interface for 1 function in a dialog that is only ever used in 1 specific fragment.

Option 2: Unit

class MyDialog: AppCompatDialogFragment(){
    lateinit var test: (String) -> Unit

    companion object {
        fun newInstance(testFunction: (String) -> Unit) = MyDialog().apply {
            test = testFunction
        }
    }
}

Then when creating the DialogFragment I could use

MyDialog.newInstance() { test -> Log.e(tag, test) }

And calling it from anywhere inside the Dialog: test("This is a test")

This is how I've been handling RecyclerViewAdapters that only require 1 function, to prevent the need for creating an interface, but since I'm directly manipulating the DialogFragment within newInstance I believe that is bad practice(all other data I pass through a Bundle), and wondering if it would cause any issues?

Option 3: Casting the fragment

This way gives me access to all public functions available in MyFragment, but does not require building an interface.

override fun onAttach(context: Context) {
    try {
        myFragment = parentFragment as MyFragment
    }catch (exception: ClassCastException){
        throw ClassCastException("$parentFragment must be instance of MyFragment")
    }
}

Now this seems like a really bad idea, but I'm not really sure why. The only reason I can think is that it won't really work if I want to use it in other Fragments. But if I know for certain this Dialog will only ever be used within this Fragment, what other potential problems could I run into?

All 3 options compile and seem to work fine on the surface. I've been using Option 1, I'd like to use Option 2 for passing single Units and not needing to create an interface, and really just curious about Option 3. I would like to know what problems I could run into if I use either of the other options, or is there a better way to do this that I'm missing?

0 Answers0