Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Navigation Architecture Component - Is Navigation Architecture Component meant to use Single Activity Only?

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:

  1. Is Android Navigation Architecture Component designed to eliminate the need of using multiple Activity in a single apps? Which mean, the whole apps just need a Single Activity and all other page will be Fragment?
  2. Does using Multiple Activities in the apps, but in the same time using the Android Navigation Architecture Component to navigate the Fragment actually violate the purpose of Android Navigation Architecture Component?

Example Scenario for Question 2:

enter image description here

like image 737
I am a Student Avatar asked Jun 25 '18 03:06

I am a Student


People also ask

Can navigation component be used for activities?

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.

What is navigation architecture component?

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.

What is single activity architecture in Android?

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.

What are navigation components in Android?

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.


1 Answers

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:

  1. 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.

  2. 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.

like image 120
zsmb13 Avatar answered Sep 27 '22 19:09

zsmb13