Android Jetpack navigation: One activity with several navigation views
from the CommonsWare Community archivesAt August 14, 2019, 9:54pm, jQrgen asked:
I am considering the new android jetpack navigation best practices where there should only be one activity in each application. I want to use one activity to implement the following menus in fragments, but I’m very insecure about what the best practices are when implementing several menus in the same application.
BottomNavigationView
(Reference repo):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.navigationadvancedsample.MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/bottom_nav"/>
</LinearLayout>
DrawerLayout (Reference repo: https://github.com/googlesamples/android-sunflower
):
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</com.google.android.material.appbar.AppBarLayout>
<fragment
android:id="@+id/garden_nav_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_garden"/>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
style="@style/Widget.MaterialComponents.NavigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"
app:menu="@menu/menu_navigation"/>
</androidx.drawerlayout.widget.DrawerLayout>
</layout>
Considering these examples from google there are no clear answers as of how to implement both views in one activity using best practices, because each example only uses one form of navigation view for their app, respectively DrawerLayout
, or BottomNavigationView
.
The questions are:
- What are the best practices for making activity with different navigation views for different fragments? For example, several fragments using
DrawerLayout
, and other fragments usingBottomNavigationView
. - What if one wants to hide both the
DrawerLayout
and theBottomNavigationView
when viewing authentication views and logging in, and display them when authenticated?
At August 15, 2019, 12:17am, mmurphy replied:
What are the best practices for making activity with different navigation views for different fragments? For example, several fragments using
DrawerLayout
, and other fragments usingBottomNavigationView
.
I cannot answer that, sorry. Among other reasons, I have never used BottomNavigationView
.
What if one wants to hide both the
DrawerLayout
and theBottomNavigationView
when viewing authentication views and logging in, and display them when authenticated?
Again, I cannot answer definitively. There are two options that I can think of:
- Have the authentication fragment be in a separate navigation graph hosted by a separate activity. The Jetpack philosophy is not so much “only one activity” as it is “not one activity per screen”. So, IMHO, it is not unreasonable to say that you use a different activity for screens where the overall structure (nav drawer, bottom nav, etc.) is significantly different.
- Use a navigation listener in the activity that hides/shows those things based upon the now-current fragment. While at the moment that might be just based on whether it is the authentication fragment or not, you could extend that to handle other scenarios as well.
I suspect that there are other alternatives; I have not had to worry about this scenario personally, so I am just brainstorming.
At August 15, 2019, 11:19am, jQrgen replied:
Thanks for the swift reply.
What are your reasons for not using BottomNavigationView
? Do you prefer an alternative?
At August 15, 2019, 11:34am, mmurphy replied:
What are your reasons for not using
BottomNavigationView
?
For the client projects that I have worked on, the graphic designers have not incorporated this sort of bottom nav, that’s all.