Navigation Controller – An Android StoryBoard
Last Updated on
Sep 12, 2018
The Navigation Architecture Components define a set of principles which helps you to build an in-app-navigation with a consistent and predictable user experience.
The Navigation Architecture Component is designed to have a single activity with multiple Fragments. The Activity hosts a Navigation Graph. If the app has multiple Activities each Activity hosts its own Navigation Graph.
Principles of Navigation:
- The app should have a fixed starting destination.
- A stack is used to represent the navigation state of an app.
- The Up button never exits the app.
- Up and Back are equivalent within the app’s task.
- Deep linking to a destination or navigating to the same destination should yield the same stack.
Implement Navigation
- Add the Google repository to the projects build.gradle(Project level).
allprojects { repositories { google() jcenter() } }
- Add the following dependencies to the app or module build.gradle(App level).
Implementation ‘android.arch.navigation:navigation-fragment-ktx:1.0.0-alpha01‘
Components of Navigation
- Navigation Graph: Blueprint of the Navigation destinations and actions that link them.
- Destinations: List of all the fragments. One can define arguments, actions and deep link URLs to these addresses.
- Host: The parent activity and entry point for the application logic.
- Actions: Specifies the destination fragment, transitions to that fragment, arguments and pop behavior.
Navigation Graph XML
- With Android Studio 3.2 android app programmers can now add a Navigation Resource File. Right click on the res directory and select New -> Android resource file. Select a title for the file and select Navigation from the Resource type dropdown.
- Now one will have a new file with a new root element type
Navigation Host Fragment
In the MainActivity layout one have to add a fragment as NavHost and define where the NavHostFragment finds the navigation graph.
PendingIntent deeplink = Navigation.findNavController(v).createDeepLink() .setDestination(R.id.android) .setArguments(args) .createPendingIntent(); |
Purpose or Use Cases of Navigation
- Fragment Transaction takes place asynchronously. Though it is generally a good thing, it might lead to a data race when another piece of code tries to read the state of the fragment, those painful IllegalStateException.
- Guidelines state that Deep Linked screen on back pressed will have to lead back to their previous logical screen. And one have to handle that manually.
- For android development company to test the android app gets much easier and simpler.
- arguments is much safer (save from refractor).
Comments