Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigate to fragment on FAB click (Navigation Architecture Components)

I have no idea how to, using the new navigation architecture component, navigate from my main screen (with a FloatingActionButton attatched to a BottomAppBar) to another screen without the app bar.

When I click the fab I want my next screen (fragment?) to slide in from the right. The problem is where do I put my BottomAppBar? If I put it in my MainActivity then I have the issue of the FloatingActionButton not having a NavController set. I also cannot put my BottomAppBar in my Fragment. I am at a loss.

like image 606
Charlie Niekirk Avatar asked Jun 28 '18 14:06

Charlie Niekirk


People also ask

What are navigation components?

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.


1 Answers

Ran into this issue today and I found out that there is a simple and elegant solution for it.

val navController = findNavController(R.id.navHostFragment)

fabAdd.setOnClickListener {
   navController.navigate(R.id.yourFragment)
}  

This takes care of the navigation. Then you must control the visibility of your BottomAppBar inside your Activity.

like image 64
Dariush Malek Avatar answered Sep 19 '22 23:09

Dariush Malek