Android Navigation Architecture Components: Actions

Android Navigation Architecture Components: Actions

In the first article, Introduction to Android Navigation Components I introduced Android Navigation components. Navigation components is a collection of libraries, a plugin, a tooling for unifying and simplifying android navigation. In the article I described major parts of navigation componets: NavGraph, NavHost and NavController. I also described how to create a navigation graph, add destinations to the navigation graph, create action between two destinations and finally implemented simple navigation from one screen to another.

This article of the series Android Navigation components focuses on actions found in a navigation graph.

What are actions?

An action is a logical connection between destinations in an app. They represent a path that user can take in your application. Actions are shown as arrows in the navigation graph. action.PNG

'A shows an action.

Click on the arrow to highlight it. This opens attributes window as shown below.

attributes.PNG

Click the Text tab to toggle to the XML view

Lets look at action id. All action ids conform to pattern @+id/action_sourceFragmnet_to_destinationFragment as shown below.

android:id="@+id/action_welcomeFragment_to_loginFragment"
Animation tab

The animation tab shows animation played as the user transits from one destination to another.

Animation.PNG

  • Enter animation is played at the destination which a user is navigating to.

  • Exit animation is played on the screen that the user is leaving.

  • Pop Enter is similar to enter but it is used when the user navigates backward. It plays on the screen that appears when the user navigates backward. For instance if the user navigates back to login screen from the main screen, pop enter animation plays on the login screen.

  • Pop Exit plays on the screen the user leaving when navigating backwards, for instance main screen in our case.

You can use default animation provided by the android framework, for example nav_default_enter_anim for enter animation. You can also create your own custom animations using XML resources and use them in the navigation components.

Arguments

This shows arguments passed from one destination to another. In the upcoming articles we will look at how to pass arguments between destinations using safe args plugin.

Pop behavior

This is the behavior of the app as the user exits by pressing the back button.

Pop To when set, as shown below, indicates the destination when the use presses the back button.

pop.PNG

To set Pop To destination click the drop down and select the destination from the list of destinations. If Inclusive is set to true, as shown in figure 1, when the use exits the app when they click the back button as shown in figure 2.

inclusie.PNG Figure 1

True.png Figure 2

Launch Options

If single top is set to true as shown below, there is at most one copy of a given destination on top of back stack. It is similar to FLAG_ACTIVITY_SINGLE_TOP in activities.

single top.PNG

In the previous blog Introduction to Android Navigation Components we implemented a simple navigation from one screen to another.

However, sometimes you want to implement a more complex navigation. For instance navigate to main screen only after successful login. In such situations Navigation Components provide navigate(action) method to perform navigation from one screen to another.

Here we require another method findNavController() which finds a NavController within an activity or a fragment.

findNavController().navigate(action)
findNavController().navigate(destination)

navigate() method requires an action ID or destination ID.

The code snippet below checks if the use has input their name and navigates to the main screen.

You can find the entire project here: Github project

Resources:

Other articles in the series Android Navigation Components

I hope this article was helpful and you look forward to the upcoming articles on passing data safely using safe args and Navigation UI patterns such as bottom navigation and navigation drawers of Android Navigation Components.

Like Share Comment Ask questions :(:(