26

I have added a new action Navigation.xml:

 <fragment
    android:id="@+id/launcher_home"
    android:name="com.example.android.codelabs.navigation.MainFragment"
    android:label="@string/home"
    tools:layout="@layout/main_fragment">
    <action
        android:id="@+id/go_to_step_one"
        app:destination="@id/flow_step_one" />
</fragment>

When calling the navigation action, if i use, the navigation works correctly :

 view.findViewById<Button>(R.id.navigate_action_bt)?.setOnClickListener(
            Navigation.createNavigateOnClickListener(R.id.go_to_step_one, null)
    )

But when calling it with a lambda, it does not work :

 view.findViewById<Button>(R.id.navigate_action_bt)?.setOnClickListener {
        Navigation.createNavigateOnClickListener(R.id.go_to_step_one, null)
    }
Levi Moreira
  • 11,917
  • 4
  • 32
  • 46
Ahmed Abidi
  • 1,047
  • 12
  • 24

3 Answers3

45

Well, that's the proper way of working. The method Navigation.createNavigateOnClickListener() returns a View.OnClickListener and according to the docs the proper way of assiging it is by using, even in Kotlin:

button.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.next_fragment, null));

If you using the method inside the lambda, the lambda itself is the click listener so you're returning a click listener inside the click listener, that's why it isn't working. It's like doing this:

button.setOnClickListener {
            View.OnClickListener {
                Toast.makeText(this, "hi", Toast.LENGTH_LONG).show()
            }
        }

What you might be intrested in is this:

 view.findViewById<Button>(R.id.navigate_action_bt)?.setOnClickListener { view ->
   view.findNavController().navigate(R.id.go_to_step_one)
}

Which will perform the navigation transition on button click.

Levi Moreira
  • 11,917
  • 4
  • 32
  • 46
1

Replace the brackets after .setOnClickListenerwith parentheses

 view.findViewById<Button>(R.id.navigate_action_bt)?.setOnClickListener {
    Navigation.createNavigateOnClickListener(R.id.go_to_step_one, null)
}

 view.findViewById<Button>(R.id.navigate_action_bt)?.setOnClickListener(
    Navigation.createNavigateOnClickListener(R.id.go_to_step_one, null)
)
0

You are doing it almost right. The only thing missing is to call 'onClick' method. Let me add my working block of code here.

override fun onClick(v: View) {
    when(v) {
        dashboardButton -> Navigation.createNavigateOnClickListener(LoginFragmentDirections.actionLoginFragmentToDashboardFragment()).onClick(v)
        registerButton -> Navigation.createNavigateOnClickListener(LoginFragmentDirections.actionLoginFragmentToRegisterFragment()).onClick(v)
    }
}

In your code, you should make a change like this.

 view.findViewById<Button>(R.id.navigate_action_bt)?.setOnClickListener {
    Navigation.createNavigateOnClickListener(R.id.go_to_step_one, null).onClick(view)
}
yasincidem
  • 51
  • 2
  • 7