Since Google I/O 2017, using the single-activity pattern along with Android Jetpack's Navigation Component has become a recommended way of developing Android applications. In this session I would like to share how the Android Navigation Component and the single-activity pattern make my life easier. The topics session include Why should we use the single-activity pattern, Essential navigation component concepts and features of Android navigation component.
1 of 74
Downloaded 11 times
More Related Content
A Single activity app with Jetpack's Navigation Component
1. A Single Activity App with
Jetpack¡¯s Navigation Component
By Boonya Kitpitak
14. 1. Navigation Graph
A navigation graph is a new type resource file that contains all
of your destinations and actions. The graph represents all of
your app's navigation paths.
27. 3. NavController
An object that manages app navigation within a NavHost.
Each NavHost has its own corresponding NavController.
NavController can be retrieved by Fragment, Activity, or View
32. 1. Passing data with Safe Args Gradle Plugin
Plugin that generates simple object and builder classes for type-
safe access to arguments specified for destinations and actions.
59. 3. Deeplink
The Navigation component lets you create two different
types of deep links: explicit and implicit.
60. Explicit Deeplink: Navigating within the app by PendingIntent
NavDeepLinkBuilder(context).setGraph(R.navigation.main_navigation)
.setDestination(R.id.berryDetailFragment)
.setArguments(url)
.createPendingIntent()
.send()
61. Implicit Deep link: Navigating from outside of the app
2 Steps
? Declare Deeplink in Navigation Graph
? Declare NavGraph in Manifest
How many of you experience ¡
I¡¯ve playing around with Jetpack¡ it would say it makes a lot of thing easier.
This architecture only have one activity ..
Start problem statement
In this video, it uses shared element transition to animate between activity.
The way to fix this is not intuitive. From what I¡¯ve heard there won¡¯t be a release to fix this problem. we might need to add these line of code forever. But this problem won¡¯t occur in the transition between Fragments.
We can treat activity as bigger component
By the reason I¡¯ve told you some of you might wanna try Single Activity but how?
By using JetPack Navigation component, we can implement single activity app really easy.
First of all I would like to show you the little application that I build as a demo for this talk.
There¡¯ll be a link to the source code of this project in my Github repo at the end of this talk.
As the talk goes you¡¯ll see how I built this app by navigation component.
This is a new type resource previously we have drawable, menu now we have navigation graph. Navigation graph represent the screens and navigation path in your app.
navigation graph file is in the resource directory under navigation folder. When you open it the file you¡¯ll navigation editor like this.
Navigation graph come with Nav editor. You can either choose to edit in design view or raw xml file.Let¡¯s take a closer look.
We call each screen a destination. The destination can be Fragment, Dialog, or BottomSheetDialogFragment. And each destination can be connected by action represented as arrow in the graph.
Here are the things that we can do with the editor. First thing is action animation.
we can add argument to each destination. It can be parcelable or primitive things like Int or string.
specify pop to destination.
Like SingleTop Flag when start an activity.
Build the app when finishing creating your graph. There¡¯ll be some auto generated class created to comfort our development.
After we have our graph set up. How could we display this graph in our activity
This is the layout file of main_activity.xml
The app:defaultNavHost="true" attribute ensures that your NavHostFragment intercepts the system Back button. navGraph is where you put the graph that you have defined in the previous step in.
Here come the third essential element we have destination and action defined then we have a container for them to display now what left is the thing to manage all of them. That thing called NavController
In activity you have to specify id of NavHost. In case there¡¯re multiple NavHost in an activity.
We can navigate to destination by ID like this.
Or we can use direction it¡¯s auto-generated class which create base on the navigation graph. But why can¡¯t we just navigate with ID. The reason is we need to use direction for passing data between destination. I will talk about this in the next topic.
This plugin will auto generate some classes to make passing data between destination safer and easier. Previously, we need to pass the data to fragment by newInstance method right?
Some are optional some are not you have to check in again before putting in argument. SafeArg can safe from this mess.
Click on destination to add argument. It can be optional or mandatory.
if there¡¯s more param it can be pass via function. In this case I define url as mandatory param so the function actionBerryListFragment¡ won¡¯t be able to compile if I don¡¯t pass item url in.
The data can be retrieved via BerryDetailFragmentArgs
Basically use can deal with every argument the BerryDetailFragmentArg
If you have worked with navigation drawer or bottom navigation, you may notice that there¡¯re some overhead. Navigation UI libs help get rid of it.
You need to add one more dependency
I¡¯ll show you the step to set up navigation drawer with navigation ui Lib.
create navHostFragment like what we did before.
In addition, you need to add NavigationView with menu specified.
It¡¯s the typical menu resource but the id need to be exactly the same as fragment id in navigation graph.
As you can see that it has exactly the same name as in menu resource.
We have everything set up now we need to bind them all together.
Navigation controller going to bind them all together.
create appBarConfiguration to control the behavior of hamburger icon and back button.
set up toolbar with NavController for showing correct toolbar label
Navigation component libs also provide the way to handle deep link
can navigate from destination in one graph to destination in another graph. We didn¡¯t use it in our example
In our example we use implicit deeplink to navigate from outside of the app like browser or other apps.
Jetpack navigation component make my life a lot easier. I can visualize the navigation in the app and compare side by side with the flow from the designer. as well as passing the data safely between screen. I understand that this architecture is not for everyone. Just like any other new architecture if the old architecture work well for you. You don¡¯t need to change it. But if you want to start new project it might worth a try.