0

I have a function:

fun test(){
    Timber.d("Button Clicked")
}

And I'm trying to pass this function to a fragment. Here is that field inside my fragment.

class MyFragment(val layout: Int) : Fragment() {
     var clickEvent1: (() -> Unit)? = null
}

And this is how I'm setting this field before beginning fragment transaction.

fragment.clickEvent1 = {test()}

My goal is to run this function on my button click inside my fragment.

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
     dialog_option_1.setOnClickListener { clickEvent1 }
}

There's an issue with the way I'm doing this because the "test" function does not run. Can someone point me in the right direction? Thanks

SomeKoder
  • 575
  • 4
  • 14
  • Does this answer your question? [Kotiln: pass data from adapter to activity](https://stackoverflow.com/questions/56826346/kotiln-pass-data-from-adapter-to-activity) – milad salimi Jan 15 '20 at 07:48

2 Answers2

0

Use

dialog_option_1.setOnClickListener(clickEvent1)

You need to pass the lambda itself to method, so you should use () parentheses instead of {}, which create a new lambda that just does almost nothing in your case.

ardenit
  • 3,610
  • 8
  • 16
0

Should be

dialog_option_1.setOnClickListener { clickEvent1() }

if you want to do it like that.

Strelok
  • 50,229
  • 9
  • 102
  • 115