Tech Blog

Coding

Getting started with the android Navigation component

By Satyabrata Dash3 min read
← Back to blog

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.

Keep reading

You may be interested in

Browse all articles →
Active Directory Federation Service
July 5, 20235 min read

Active Directory Federation Service

Active Directory Federation Service (ADFS) uses claims-based identity model to maintain application security and to implement federated identity. ADFS is a concept of Windows Server operating system, that provides users with single sign-on access to the application, those are located across organizational boundaries. For Example: We have 2 organizations – Organization 1 and Organization 2. […]

By Abinash MishraRead More →
Remote Access to Microsoft SQL Database Server
April 5, 20232 min read

Remote Access to Microsoft SQL Database Server

Remote access to a SQL server is important because it allows users to access the server from anywhere, at any time. This enables remote workers to access the data they need when they need it, even if they are not physically in the same location as the server. It also allows IT admins to access […]

By Malkeet SinghRead More →
Types of Load Balancer in AWS
February 6, 20235 min read

Types of Load Balancer in AWS

What is Load Balancing ? Load balancing is a critical component of any scalable and highly available Cloud or On-premise architecture. It helps distribute incoming traffic across multiple servers, improving the availability and scalability of a website or application. In the cloud, load balancing is offered as a service by various cloud providers such as […]

By Keerthik ShenoyRead More →