5

I have an activity that has a fragment layout dedicated to the toolbar. In the center we have a normal nav_host container.

The activity contains one fragment that correctly shows the hamburger. If I click a button it gets replaced from a second fragment that replace the old toolbar with a new one. After clicking back the new toolbar is replaced by the old one.

The problem is that the hamburger icon doesn't show up and the toolbar doesn't work as expected. Ideas?

This is the code in the activity:

open fun setupToolbar() {
    initialToolbar = ToolbarFragment()
    setToolbarFragment(initialToolbar){
        setNavController()
    }
}

fun setToolbarFragment(fragment: Fragment, callback: () -> (Unit) = {}) {
    supportFragmentManager
            .beginTransaction()
            .replace(R.id.toolbarContainer, fragment)
            .runOnCommit {
                callback.invoke()
            }.commit()
}

private fun setNavController() {
    val navController = findNavController(R.id.nav_host_fragment)
    val appBarConfiguration = setFragmentsWithHamburgerMenu(navController)
    toolbar.setupWithNavController(navController, appBarConfiguration)
}

fun setFragmentsWithHamburgerMenu(navController: NavController): AppBarConfiguration {
    return AppBarConfiguration(
            setOf(
                    R.id.analyticsFragment,
                    R.id.routinesFragment,
                    R.id.currentRunFragment,
                    R.id.myMapsFragment,
                    R.id.myRobotsFragment
            ),
            drawer_layout
    )
}

This is the code instead of the second fragment:

override fun onResume() {
    super.onResume()
    val fragment = SearchToolbarFragment(searchHint, this::onQueryTextChange)
    baseActivity.setToolbarFragment(fragment) {
          // other stuff
    }

}

override fun onPause() {
    super.onPause()
    baseActivity.setupToolbar()
}

This instead is my activity layout:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:bind="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable name="viewModel" type="[package].DashboardViewModel" />
    </data>

    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <FrameLayout
                android:id="@+id/toolbarContainer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toTopOf="parent"/>

            <fragment
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:id="@+id/nav_host_fragment"
                android:name="androidx.navigation.fragment.NavHostFragment"
                app:defaultNavHost="true"
                app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/toolbarContainer" />

            <com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/bottom_nav"
                style="@style/BottomNavigationView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white"
                app:itemBackground="@color/white"
                app:itemTextColor="@color/button_view_item"
                app:labelVisibilityMode="labeled"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:menu="@menu/menu_bottom_nav" />

            <LinearLayout
                android:id="@+id/progressMaskLayout"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/toolbarContainer">

                <include layout="@layout/progress_mask" />
            </LinearLayout>

        </androidx.constraintlayout.widget.ConstraintLayout>

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true">

            <include
                bind:viewModel="@{viewModel}"
                layout="@layout/element_navigation_bar"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

        </com.google.android.material.navigation.NavigationView>
    </androidx.drawerlayout.widget.DrawerLayout>
</layout>
Filnik
  • 352
  • 1
  • 12
  • 33
  • 1
    Could you be a bit more specific about the issue you're having? Does the toolbar show correctly? Is a different icon displayed instead of the hamburger-menu? Etc... – tynn May 20 '20 at 19:34
  • You should post a screenshot of what happened. In my case a hamburger icon animation didn't fully render when I added fragments to an activity. If I don't mind, I added some time to draw full animation. – CoolMind May 26 '20 at 15:34
  • Does this answer your question: https://stackoverflow.com/questions/30824324/clicking-hamburger-icon-on-toolbar-does-not-open-navigation-drawer/30824390 – 10 Rep May 27 '20 at 01:50

2 Answers2

2

To avoid the problem I just added a fragment over the previous one and I removed the added one.

Like this:

fun addToolbarFragment(fragment: Fragment, callback: () -> (Unit) = {}) {
    supportFragmentManager
            .beginTransaction()
            .add(R.id.toolbarContainer, fragment)
            .runOnCommit {
                callback.invoke()
            }.commit()
}

fun removeToolbarFragment(fragment: Fragment, callback: () -> (Unit) = {}) {
    supportFragmentManager
            .beginTransaction()
            .remove(fragment)
            .runOnCommit {
                callback.invoke()
            }.commit()
}

In this way, all the customization done in the first toolbar won't be lost.

Filnik
  • 352
  • 1
  • 12
  • 33
0

First, you're using Android jetpack navigation component but also using fragment manager to manually do fragment transactions. You're defying the purpose of using the jetpack navigation component. That's the whole point of using the navigation library so that we don't have to manually do the fragment transactions. What you have to do here is put your toolbar in the main_activity.xml so that all fragments can inherit it.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<layout 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">

    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/Drawer_Main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.coordinatorlayout.widget.CoordinatorLayout
            android:id="@+id/Layout_Coordinator_Main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true">

            <com.google.android.material.appbar.MaterialToolbar
                android:id="@+id/Toolbar_Main"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary">

                <TextView
                    android:id="@+id/Toolbar_Main_Title"
                    style="@style/Locky.Text.Toolbar.TitleText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="@string/app_name" />

            </com.google.android.material.appbar.MaterialToolbar>

            <androidx.core.widget.NestedScrollView
                android:id="@+id/Nested_Scroll"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="?attr/actionBarSize"
                android:fillViewport="true">

                <fragment
                    android:id="@+id/Navigation_Host"
                    android:name="androidx.navigation.fragment.NavHostFragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:defaultNavHost="true"
                    app:navGraph="@navigation/navigation_drawer_main" />

            </androidx.core.widget.NestedScrollView>
            <com.google.android.material.floatingactionbutton.FloatingActionButton
                android:id="@+id/FAB_Add"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|end"
                android:layout_margin="@dimen/fab_margin"
                app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior"
                app:srcCompat="@drawable/ic_add" />

        </androidx.coordinatorlayout.widget.CoordinatorLayout>

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/Navigation_View"
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:clipToPadding="false"
            android:paddingStart="0dp"
            android:paddingEnd="16dp"
            app:headerLayout="@layout/drawer_header"
            app:menu="@menu/menu_drawer_main" />

    </androidx.drawerlayout.widget.DrawerLayout>

</layout>
Mervin Hemaraju
  • 1,921
  • 2
  • 22
  • 71
  • I've added my main activity layout. I do put the toolbar in my main_activity.xml. Am I doing it in the wrong way? – Filnik May 15 '20 at 08:18
  • I updated my answer with some codes. Do something like this and then use navigation component to change your fragments. If you need to modify the title of the toolbar, just do it in your main activity. – Mervin Hemaraju May 15 '20 at 13:47