Using the Navigation component with multiple activities - android

In the Android docs, it states:
The Navigation component is designed for apps that have one main activity with multiple fragment destinations. The main activity is associated with a navigation graph and contains a NavHostFragment that is responsible for swapping destinations as needed. In an app with multiple activity destinations, each activity has its own navigation graph.
Does this mean that you cannot use the Navigation component to navigate from one activity to another? That appears to be the case.
Second question: If I create an app that uses the navigation drawer, the default code that created when you add an activity that is to have a navigation drawer already has code for managing navigation from one drawer item to another. So is the Navigation component also kind of useless here?
Does Google want us to be creating only single activity apps?

Does Google want us to be creating only single activity apps?
Single activity architecture is something you can move towards. It is not restricted (just recommended) by Google. The architecture has its own advantages and drawbacks. You don’t have to tear up your entire app simply to add Navigation Component. Evaluate and Decide whether it’s worth the pain.
Does this mean that you cannot use the Navigation component to
navigate from one activity to another
No, You can use Navigation Component to replace startActivity calls. Simply add your Second Activity Nav Graph to the First Activity Nav Graph and use the nav controller to navigate between the two.
findNavController().navigate(directions)
Here is a migration guide for it https://developer.android.com/guide/navigation/navigation-migrate#add
In cases, where you want to use a different activity, you can evaluate whether you need a different activity or a different task.
If I create an app that uses the navigation drawer, the default code
that created when you add an activity that is to have a navigation
drawer already has code for managing navigation from one drawer item
to another. So is the Navigation component also kind of useless here?
or
instead of using the default code for a navigation drawer to build
your own navigation drawer that is more inline with the Navigation
component
The thing is you don’t have to build a custom component or anything complicated. Actually, use of the Navigation Component (with the help of NavigationUI class) simplifies the code for the drawer layout and its listeners.
At this link, the documentation helps you implement the Navigation component when using Navigation Drawer and Bottom Navigation View.
With regard to the generated templates, those are outdated and need an upgrade.
References:
https://developer.android.com/guide/navigation/navigation-migrate
https://developer.android.com/guide/navigation/navigation-ui

Short Answer is Unnecessary because:
In Navigation Component idea, you need to have 1 + 3 parts and unlimited fragments.
You can watch Google Navigation Component Video.
Only one Activity.
Single Activity
These are working in the one Activity (Single Activity).
Navigation graph
NavHostFragment
NavController
Why Unnecessary? Because, all parts of "1 + 3" connected with each other.
Details:
Navigation graph is connected with NavFostFragment. Moreover, NavFostFragment defines in XML file of Single Activity. Also, NavController defines by NavController as "navHostFragment.navController".
However, if you really really want to use Navigation Compenent for Activities, you need to use add fragments in Activities.
For Example:
[Activity_A + Fragment_A] and [Activity_B + Fragment_B]
The idea of solution is:
For Activity_A to Activity_B: Navigate Fragment_A -> Activity_B
OR
You can migrate. For Activity_A to Activity_B: Navigate Fragment_A -> Activity_B
More detail: Migrate to the Navigation component by Google

Related

Show Navigation drawer in every activity - android studio

I have a problem...I am developing an app in android studio.In the meantime,there is already a navigation drawer in it.But the problem is, it only appears in the home screen.When it goes to another screen, the navigation drawer is not shown. I am new to android studio but i know some of Java languages.Can anyone help me with providing anything that would help me make the navigation drawer show in all of slides and pages in my app.
from docs
Navigation Drawer
The navigation drawer is a panel that displays the app’s main
navigation options on the left edge of the screen. It is hidden most
of the time, but is revealed when the user swipes a finger from the
left edge of the screen or, while at the top level of the app, the
user touches the app icon in the action bar.
for achieve what you asked you have to use Fragments
Fragment represents a behavior or a portion of user interface in an
Activity. You can combine multiple fragments in a single activity to
build a multi-pane UI and reuse a fragment in multiple activities. You
can think of a fragment as a modular section of an activity, which has
its own lifecycle, receives its own input events, and which you can
add or remove while the activity is running (sort of like a "sub
activity" that you can reuse in different activities).
see this examples for more details
1. Navigation Drawer - android hive
2. Navigation Drawer exp 2
3. Navigation Drawer exp 3
Refer this answer also
Answer
You have to add fragments in that activity where the navigation drawer exist. Whenver the user will click on the option in the navigation drawer the view should be changed by replacing with the required fragment.So by using the fragments user will stay on the same activity but just the views will be changed in that activity. you can refer to the fragments documentation provided by android developers. https://developer.android.com/guide/components/fragments.html

Using navigation drawer for multiple activities re-creates the drawer. How do I make it stay common?

I've created a base activity for navigation drawer and every other activity will extend this to display the drawer.
Doing this will re-create the drawer for each activity.
Is there any kind of workaround for this such that a common drawer is used across all the activities?
EDIT:
Source of what I've referred to do this:
Same Navigation Drawer in different Activities
I prefer using fragments instead of activities when dealing with a navigation drawer, this will both be more efficient and good looking.
A guide on how to do this can be found at:
fragment-navigation-drawer-guide
In short no, or at least: You should not do that.
Navigation drawers are usually to be used with fragments that you swap, they should not create new activities, since the drawer can not (or should not) be shared.
You should overthink your navigation, and use fragments instead where appropriate. Following the design guidelines, again, navigation drawers should be the top most navigation, and no other activities should have one either.
The answer on how to use a common drawer would hence be to use an activity with the drawer managing different fragments.
If you absolutely must, you can detach the navigation drawer view from the layout, keep the reference some place, then reuse the same view in another activity. This is really dirty and as mentioned, you should not do this.

Need clarification on how the native Navigation Drawer works with Fragments

I'm used to using a 3rd party library for implementing a navigation drawer (sliding menu) in Android, but I want to use the native version from now. I went through the tutorial (http://developer.android.com/training/implementing-navigation/nav-drawer.html) which is pretty straight forward.
I typically extend the 3rd party navigation drawer to each activity by defining the configuration in a base class. The new nav drawer, however, swaps fragments in and out, which my research indicates is the standard way of managing your displays.
This seems fine, but my app has a fairly complex hierarchy of pages and navigation. Like most apps, it contains more fragments than just the ones in the menu.
So if I have 3 nav drawer items for fragments A, B, and C, and I can only load fragment D from fragment C, do I handle that navigation logic in the activity where I configure the nav drawer? It seems like kind of a nightmare to have one container to swap out an indefinite number of fragments, especially if the work flow is deep.
From what I can gather on Stack, there seem to be a lot of people who are familiar with extending a 3rd party drawer in each Activity, but when they switch over to the native version there is confusion.
So to summarize, I understand the fragment swapping aspect of the navigation. I just don't understand how the rest of the work flow navigation would work, say if I had several detail screens below a nav item fragment. If anyone can give me some hints on how best to approach this scenario, maybe I can experiment and post some code for future readers.
For navigation in Android there is always one thing you have to remember:
If you stay on the same level of the navigation hierarchy, for
example when swiping through pages, you use Fragments.
When you move up and down in the navigation hierarchy, for example
going to a detail view, you would start a new Activity and displayed the
Fragment with the detail content in it.
A NavigationDrawer is used for top level navigation in your app to quickly navigate between different parts of your app. It's kind of like a main menu. With that in mind you need to determine if a NavigationDrawer makes sense in your app. It's all about how the user should navigate through the content. If there is just one path for the user to follow for example if you start out with just one screen and from then on the user can just go deeper and deeper in the navigation hierarchy from one detail view to the next than a NavigationDrawer does not make much sense. But if there are multiple paths the user can take that lead into different, independent parts of your app without one dedicated start screen on which everything else depends than a NavigationDrawer sounds pretty reasonable.
You can look at Google apps like Gmail, Drive or Google+ to see how a NavigationDrawer is supposed to be used.

Best practices on Navigation Drawer use

I was reading about Navigation Drawer and it was not until now that I came into this question: If my top views are going to be accessible through the Navigation Drawer, should I code each of them as Fragments or as Activities?
I want to know the way it was intended to be used, with Fragments or with Activities
The best way is to use Fragments.
When you click on an item, you should always replace the Fragment of your Main Activity without adding the elements in the Back Stack. This will create a seamless dynamic navigation instead of launching new activities and managing the drawer into them.
Here are some references
Google's design guidelines
Google's development guidelines

Android Navigation Drawer Design

I have an existing application that has about 25 activities that are navigated to from a "dashboard". I would like to switch and start using the Navigation drawer and fragments. I have gone thru the Nav Drawer design pages online and the example app. My question is what is the best way to convert (structure) my app to fit the Nav Drawer pattern. If I switch my activities to be fragments and use a main activity to replace each fragment as navigation happens, but not sure if that is good b/c for a tablet layout, I might want multiple fragments on my view and not sure if this will limit me. If I go with the other direction I was thinking, keep all my activities and just switch the necessary ones to fragments for tablets but I would need each activity to create the navigation drawer (I think ?) which in my case the drawer is dynamic based on server data. Any suggestions would be great.
Thanks
Brandon
Navigation drawer has to be created for each activity, although you could inherit Activity and create a parent class that handles navigation drawer specific code if code duplication is a concern.
Using a drawer does not limit you to use one fragment per screen, just listen to onClick in drawer and initiate as many fragment transactions as you need.
When it comes to structuring your app, there is no universal advice, I would recommend you to watch Google I/O 2013 talk - Structure in Android App Design. Navigation Drawer is kind of the main theme of the talk.

Categories

Resources