I am a Student
I am a Student

Reputation: 1590

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

Upvotes: 14

Views: 3770

Answers (1)

zsmb13
zsmb13

Reputation: 89668

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.

Upvotes: 10

Related Questions