Getting started with the android Navigation component


The navigation from one screen to another in android applications is made easier through android Navigation component. With the Navigation component, we get a collection of libraries and tools that simplifies navigation and gives the programmer a visualization of how the navigation works.

Advantages of using Navigation component

     1.  Simplified setup and common navigation

     2.  Handles backstack

     3. Typesafe argument passing

     4. Centralized and visualizes navigation and many more..

In this blog let’s discuss an example of a simple Navigation. In the below screenshot we can see a simple game flow. Here we are moving from the StartGameFragment to GameFragment and from GameFragment to FinishFragment on button clicks, “Lets see how to implement”.

Dependencies

Lets add these in our app level gradle file.

def nav_version = “2.4.1”

 // Java language implementation

   implementation “androidx.navigation:navigation-fragment:$nav_version”

   implementation “androidx.navigation:navigation-ui:$nav_version”

Let’s understand how all this works with a single Activity and 3 fragments.Simply create 3 fragments and their respective layout files like highlighted in below screenshot.

Now we have to create a navigation resource file under res directory

Once created it will show a blank screen so we have to add our fragments to this navigation graph so that we can have a visualization of all fragments. 

Click on plus icon as shown below and select all the fragments that we have created.

once selected the screen will look like this..

Next link the fragments to one another as shown in below screenshot(Screenshot-1) and once linked click on any arrow upon click on the right side in the attribute section an navigation id will be created(Screenshot-2) which we will use to navigate from one fragment to another. In this case our id is action_startFragment_to_gameFragment we will use it to move from StartGameFragment to GameFragment.

(Screenshot-1)

(Screenshot-2)

Once done let’s move to our activity_main.xml and add a FragmentContainerView, we will use this FragmentContainerView to swap in and out fragments on top of the Activity.

<androidx.fragment.app.FragmentContainerView

        android:id=”@+id/fragmentContainerView”

        android:name=”androidx.navigation.fragment.NavHostFragment”

        android:layout_width=”match_parent”

        android:layout_height=”match_parent”

        app:defaultNavHost=”true”

        app:navGraph=”@navigation/nav_graph” /> 

//nav_graph: here we are linking the navigation resource which we had created earlier

Now, if i want to navigate from StartGameFragment to GameFragment, i can do it like this in StartGameFragment

@Override

    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

        Button lObjStartButton = view.findViewById(R.id.button);

        NavController lOjNavController = Navigation.findNavController(view);

        lObjStartButton.setOnClickListener(new View.OnClickListener()

        {

            @Override

            public void onClick(View v)

            {

                  lOjNavController.navigate(R.id.action_startFragment_to_gameFragment);

            }

        });

    }
In the above code we have basically created an object of NavController and using the object we are calling lOjNavController.navigate() method by passing the navigation id, which we had got earlier from the navigation graph. This will result in navigation from StartGameFragment to GameFragment leading to a  simple navigation from one screen to another.

Leave A Comment

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