Best practices on Navigation Drawer use - android

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

Related

Using the Navigation component with multiple activities

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

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.

Applying navigation drawer throughout the app in Android?

I had an app which was developed 2 years back, now I want to add navigation drawer to that app, but the problem is in that app there are 15 Activities, so do I need to apply the Navigation drawer for all 15 Activities? or is there best way to implement this.?
The navigation drawer items are common for whole app.
Can anyone suggest me the best way to implement this.
A way you can do it, since Navigation Drawers are just list fragments, is create a list fragment and recall that same fragment in all the activities that you want it in. You can do this using the XML Layout Editor to place the fragment in the layout.
Having so many activities, however, makes it a lot of work (especially if your app is designed for multiple sizes and each activity has a layout).

Navigation Drawer and Fragments best practice

I am going to use Navigation Drawer in my app. I have read about most common approaches of building such apps, a lot of people suggest to change only framgent while selecting different items in navigation drawer. So instead of openning new activity it is better to change fragment of current activity ? Am I right ?
The problem is that I have main activity with two fragments now.
What is the best practice to follow in this case.
To open new activity on item click in navigation drawer ?
To use nested fragments, because my activity already has 2 fragments, so maybe to create some wrapper fragment to nest these two fragments or more if it will be another page.
Please suggest what is the best practice to implement this design pattern in my case.
Technically, the correct way is to change only the fragment while selecting different items in navigation drawer, which would be possible even with only two fragments. There is usually no reason to use nested fragments.
Obviously, there are many ways to do it, and from the UI perspective, there are many possible designs (Navigation Drawer, tabs, sliding pager, etc.)

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