I currently learning on the new Android Navigation Architecture Component (https://developer.android.com/topic/libraries/architecture/navigation/).
I kind of confuse with its motive and concept, here are my uncertainties:
Example Scenario for Question 2:
The Navigation component uses an activity as a host for navigation and swaps individual fragments into that host as your users navigate through your app. Before you can start to layout out your app's navigation visually, you need to configure a NavHost inside of the activity that is going to host this graph.
Android Jetpack's Navigation component helps you implement navigation, from simple button clicks to more complex patterns, such as app bars and the navigation drawer. The Navigation component also ensures a consistent and predictable user experience by adhering to an established set of principles.
Briefly, the Single-Activity Architecture is the architecture that has only one Activity or a relatively small number of Activities. Instead of having one Activity represent one screen, we view an Activity as a big container with the fragments inside the Activity representing the screen.
The Navigation Component is made up of three major parts: This includes all of the locations in your app, referred to as destinations, as well as the possible paths a user could take through your app. NavHostFragment (Layout XML view) — This is a unique widget that you can include in your layout.
In theory, the Navigation library supports any architecture you might want to use. Out of the box it can handle Activities and Fragments as navigation destinations, but you can plug in your own solution by implementing your own Navigator (as an example, see this article).
However, quoted / paraphrased from the Google I/O talk on Navigation:
What is my Activity actually meant to do?
Right now, some apps are very Activity-heavy, some are Fragment-heavy, or completely in a different system. We're moving towards a model where the Activity is more just an entry point into your app, rather than it being the owner of the content of your app. It's actually just going to store global state, for example global navigation like a navigation drawer or the bottom bar.
So Google does recommend having just a couple Activities for your app, because you only really need them to serve as entry points. For example, you can have one that opens from the launcher, and another that's opened by deep links. After that, when your app is started, you can do everything else inside it with Fragments.
To summarize and directly answer your two questions:
The Navigation Architecture Component isn't "designed to eliminate the need to use multiple Activities" per se, but it's something Google recommends doing when you're using it.
You can absolutely still use multiple Activities and multiple Fragments mixed together. You can even use a single Activity with purely View based navigation if you like. It's all up to you. If you find the Navigation library useful in combination with how you architect your app, use it.
The tooling of the library might not be that great for custom destinations (for example, the visual editor will probably only support Activities and Fragments for the time being), but you can use it however you'd like from code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With