Multiple containers for navigation component - android

Is it possible to share navigation graph between two (or more) containers?
What I would like to achieve is to have two containers (NavHosts):
one smaller that is above bottom navigation and below toolbar
second is full screen.
I would like to have one navigation graph, because fragments from one container can trigger actions from other one. Otherwise it (having more navigation graphs that interacts with each other) will become untidy and problems created by library will surpass advantages.

Seems like you cannot, however you can have nested navigation: i.e. wrap your toolbar navigation inside of fullscreen (of vice versa) and playing with
navController.popBackStack(<id of nested item>)

Yes, it is possible. Take a look at this sample. This is an implementation of Android Navigation component with bottom navigation view with separate history for each tab. You can use the concept and change it the way that fits your app

Related

Android Navigation with nested bottom navigation

I'm new to Android Navigation Component and want to understand how can I use it in my relatively simple scenario where I want to have a single activity approach. I obviously want the system to handle the back stack, also for bottom navigation.
What I need is 2 very simple cases:
1. Mixed destinations
Some of the destinations in my navigation graph have bottom navigation, and some not. Imagine that I have a login flow without a bottom navigation and then move to the "real app" where I have bottom navigation. Google says put bottom navigation, drawer, action bar outside of navigation graph, which means they are shown for all destinations.
Is this supported by Navigation Component? Without dirty hacks of hiding/showing bottom nav.
2. Full screen popups
Another question - is multiple navhosts supported? Imagine I have a UI with bottom navigation and action bar, which are outside of the nav host area. But then I need to show a full screen popup, like a dialog or a resource selection screen, which will also go over the bottom nav and action bar.
Can I do this as a destination in navigation graph?
For both questions any conceptually supported solution is good for me, including switching graphs/hosts during navigation.
As per the Listen for navigation events documentation:
As an example, you might have common UI elements that you intend to show in some areas of your app while hiding them in others. Using your own OnDestinationChangedListener, you can selectively show or hide these UI elements based on the target destination
So yes, you can selectively show or hide elements of your activity's UI when you move to certain destinations, such as your login screen.
As per the Create a destination from a DialogFragment documentation:
If you have an existing DialogFragment, you can use the <dialog> element to add the dialog to your navigation graph
This also supports other types of DialogFragment such as a BottomSheetDialogFragment.

Navigation Component - How to Nest Navigation Graph?

I am looking for example with Navigation Component in Android that will use multiple navigation graphs and then show how to nest them together?
Could you give any example or hint? It is new feature in Android, and there are only simple Single Activity examples with NavHostFragment, but how to use multiple navigation graphs correctly nest them or switch from one nav graph to another. What are the best practices?

Android, reusable toolbar for navigation - where to put code?

I want to implement a toolbar for navigation which appears at the bottom of almost all activities in my app. It will have a fixed amout of elements (four).
Screenshot:
(certain activities will still have their own toolbar at the top):
In order to make it somewhat reusable, I've created a separate layout file for the toolbar which allows me to include it on multiple activities with:
<include android:id="#+id/toolbar_main" layout="#layout/toolbar_main" />
Every item of the toolbar leads to another activity, which means it acts as a navigation throughout the whole application.
However, as I want to use it on multiple activities, I'm not sure on what would be the correct place for the Java-Code behind the onClick-Events of the menu items. I've seen approaches using a base-class which can be extended by all activities using this navigation toolbar. I've also thought about not using a toolbar at all and creating a fragment for the navigation. I haven't used fragments yet, however, they seem to have their own code-behind class in addition to their design .xml, which would be suitable for the click-Events. Any suggestions? I might add, that I want to center and stretch the navigation bar later on, which seems to be not so easy using a Toolbar with associated menu.
Make A Base activity, which will be extending by all your other activities. Write all your logic related to that toolbar on Base Activity. Thats all.
You could use the TabLayout with ViewPager. Place them in main Activity, and use Fragment for every root screen.

Android navigation drawer - two levels

I am looking to replicate the iOS app my company makes and on tablet in landscape they have a second nav drawer slide out from the first for the second level of categories.
e.g. in the first level they have clothes, shoes, accessories, then if you click clothes a second one appears scrolling to the right from the far right of the first nav bar, that then shows things like shirts, t-shirt, jeans, jumpers etc.
Is this possible in android? And if so, is it the accepted way of doing things?
Thanks
on tablet in landscape they have a second nav drawer slide out from the first for the second level of categories.
That's not a nav drawer, then, at least as defined by Google.
Is this possible in android?
Sure, just not using DrawerLayout. Execute a FragmentTransaction to slide in the second list adjacent to the first list, neither of which are in a DrawerLayout.
And if so, is it the accepted way of doing things?
For a two-tier structure, I suspect that you will see other patterns used:
ExpandableListView
tabs for the first tier, and simpler master-detail for the second tier
action bar list navigation for the first tier, and simpler master-detail for the second tier
etc.
I can't even rule out DrawerLayout as being the implementation of the first tier (with traditional master-detail for the second tier), though the stuff in that list does not strike me as fitting the usage pattern for a navigation drawer.
Sure, It is possbile. But it is not a practise using in android.
If you want to implement this design, the best way is to use two fragments instead of navigation drawer and use FragmentTrasation to provide the animations.

What is the difference between the SlidingMenu library and the Android Navigation Drawer?

The SlidingMenu library is an excellent third party library and I've already used it for a long time. Now I know Android provides a new navigation pattern using Navigation Drawer. It looks like the sliding menu. So is there anyone who already uses these two both? What is the difference and what are the pros and cons? Thanks a lot.
SlidingMenu library is a third party api which uses a RelativeLayout inside. The main advantage is customization according to your requirement. Buy your layouts have to be based on a viewgroup, unfortunatly this negates the <merge> optimisations.
Navigation Drawer is available in the Support Library of android it uses DrawerLayout inside. The main advantage is improved performance.
They also have different visual effects. SlidingMenu looks like horizontal scroll view. Sliding it in will push the main content out.
Pros :
It comes with cool entrance / exit animations for the menu content.
Depending on what Activity you use it on, it can be placed below the Action Bar or next to it (pushing the Action Bar too)
You can explicitly set the touch mode via a setter: margin or full screen. In full screen mode touching anywhere on the screen will open the menu. In margin, only when you slide from the edge of screen will the menu open.
Cons :
You can only control the shadow of the side menu
Navigation Drawer / Drawer Layout looks like an additional top level view in a frame layout. Sliding it in will mask the main content.
Pros :
If you use v4 support lib then it's already there.
You can control both the side menu shadow and obscure the main
content via setScrimColor e.g when the drawer is opened, a fade-in
alpha layer will appear above the main content. This is good to visually separate the content and the menu especially when both have a same color.
Cons:
It can only be placed below ActionBar
There is no setter for touch mode and by default you can only do margin touch mode. Doing a full screen touch mode (like Youtube) involves a lot of work
I think the best advantage is that It is official Google code, I mean it just works and works excellent.
The main disadvantage is that it is very basic to use, I mean... you cannot put two navigation drawer in the same activity or fragment, you can only use one in left and that's it.
You already said it yourself. Sliding menu is third party. Navigation drawer is official. Both have the same purpose, but third party libraries might implementing it slightly differently, depending on which one you use.

Categories

Resources