Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation Architecture Component - Action Navigation with lambda expression

Tags:

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)     } 
like image 626
Ahmed Abidi Avatar asked May 14 '18 11:05

Ahmed Abidi


People also ask

What is navigation architecture component?

The Navigation component contains a default NavHost implementation, NavHostFragment , that displays fragment destinations. NavController : An object that manages app navigation within a NavHost . The NavController orchestrates the swapping of destination content in the NavHost as users move throughout your app.

How can navigation component be used in activity?

To add a new destination using the Navigation Editor, do the following: In the Navigation Editor, click the New Destination icon , and then click Create new destination. In the New Android Component dialog that appears, create your fragment. For more information on fragments, see the fragment documentation.

What is popUpTo and popUpToInclusive?

When navigating back to destination A, we also popUpTo A, which means that we remove B and C from the stack while navigating. With app:popUpToInclusive="true" , we also pop that first A off of the stack, effectively clearing it.

What is NavController in Android?

NavController manages app navigation within a NavHost . Apps will generally obtain a controller directly from a host, or by using one of the utility methods on the Navigation class rather than create a controller directly. Navigation flows and destinations are determined by the navigation graph owned by the controller.


1 Answers

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.

like image 96
Levi Moreira Avatar answered Sep 30 '22 18:09

Levi Moreira