android – Navigation Architecture Component – Action Navigation with lambda expression-ThrowExceptions

Exception or error:

I have added a new action Navigation.xml:

        app:destination="@id/flow_step_one" />

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

            Navigation.createNavigateOnClickListener(, null)

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

 view.findViewById<Button>( {
        Navigation.createNavigateOnClickListener(, null)
How to solve:

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(, 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>( { view ->

Which will perform the navigation transition on button click.

Leave a Reply

Your email address will not be published. Required fields are marked *