This time Google Developers have introduced many interesting concepts to make native Android development more efficient and faster. One of them is the Navigation Architecture Component, part of Android Jetpack and the new AndroidX package. It makes easy to implement the navigation in your Android app. It totally changes the scenario of navigation between fragments and also suggest to use of single-Activity architecture as the preferred architecture for Android Jetpack.
This architecture also has support for deep links and fragments, which will be create a cleaner, more user friendly experience.
What, Why, and How.
Three common questions arise in everyone’s mind.
What is Navigation Architecture Component?
Why is it invented ?
How can we implement it in our app?
- The navigation architecture component is here to replace the tedious maintenance of transaction of activities and fragments.
- It is invented because handling the activities and fragments via traditional way is critical and lengthy process.
- For the answer of the third question we need to dive into to deep info.
Prerequisite for the trip
*Note :For now Navigation Architecture Components is limited to Android Studio 3.2 Canary 14 or higher.
First of all you need to add the navigation fragment and UI libraries to your project. They are available via the google() repository.
If you want to pass argument as bundle within fragments you need to include the safeargs navigation into build.gradle classpath. This plugin helps to generate code that allows type-safe access to properties used in argument bundles.
In your project build.gradle, now you are able to apply the Gradle plugin as you normally do.
apply plugin: 'androidx.navigation.safeargs'
Now it’s time to roll…!
- First of all open project window then right-click on the resource(res) directory and select New from Android resource file.
- Mention the name in File name field, such as “navigation_graph”.
- Set Navigation from the Resource type drop-down list.
- Tap OK. The following occurs:
- A navigation resource directory will be added within the res directory.
- A navigation_graph.xml file will be available within the navigation directory.
- The navigation_graph.xml file will open in the Navigation Editor. This xml file fills your navigation graph.
- Tap on Text tab to toggle to the XML text view. The XML for an empty navigation graph looks as shown below:
123<?xml version="1.0" encoding="utf-8"?><navigation xmlns:android="http://schemas.android.com/apk/res/android"></navigation>
- Tap on Design to get back to the Navigation Editor.
First Hold to look Navigational Editor
The Navigation Editor’s divisions are:
- Destinations list – all destinations are available in the Graph Editor.
- Graph Editor – contains visual design of your navigation graph.
- Attributes Editor – contains attributes associated with destinations and actions in the navigation graph.
The first step in creating a navigation graph is to identify the destinations in your app. You can create blank destinations or create destinations from fragments in an existing project.
Notice : The main activity “hosts” the navigation graph. In an app with multiple activity destinations, every other activity hosts its personal navigation graph.The Navigation Architecture Component is here for apps that have single main activity with more than one fragment destinations.
Connecting Fragments :
Provide the Fragment Transitions animations for four states
- Enter -When entering into next fragment
- Exit -When exiting from current fragment
- Pop Enter -When entering to previous fragment from current fragment
- Pop Exit – When exiting from current fragment to previous fragment
Androidx also provide 4 default animations
We can also add custom animation to anim folder in resources. It will automatically detect that particular animation into its transitions dropdown
Launch Option :
We can set the default launch option for fragment with this property
- Single Top
Launch a navigation fragment as a Single Top. Using this, the system holds only one fragment on the top as like activity do.
Launch a navigation fragment as a document. Doing this, the system stores fragment document entry for its own use and it will not create the document while transferring to that fragment again.
- Clear Task
Launch a navigation fragment as a Clear Task. This will clear the backstack.When moving from one fragment to another fragment, the backstack will be cleared as shown below.
Here is xml view of navigation graph
<?xml version="1.0" encoding="utf-8"?>
<!--auto generated action when connecting fragments from design we can also add via xml coding-->
<!--argument can be also passed from xml-->
The navigation library change the experience to decouple routing logic. The easy implementation actions and type-safe arguments are best additional functionality for robust API.