0

I've created a DrawerNavigation which has a MenuItem (Home) that has a BottomNavigationBar. For the MenuItem "Hinzufügen" in the BottomNavigationBar I've added custom MenuItems for the Toolbar within the corresponding Fragment (AddFragment.java)

AddFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
    }
    setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.clear();
    inflater.inflate(R.menu.save_toolbar_menu, menu);
}

The Toolbar items for all other fragments will inflated in the MainActivity:

MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.toolbar_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

Drawer Navigation

"Hinzufügen" view

Problem:

When I switch between the BottomNavigationBar items, the Toolbar Menus works as expected. That means, the "Hinzufüen" menu displays the R.menu.save_toolbar_menu and when I select another MenuItem, the R.menu.toolbar_menu will inflated into the toolbar.

Selected another BottomNavigationitem

BUT: When I select "Hinzufügen" in the BottomNavigationBar and then select a MenuItem from the DrawerNavigation (lets say "Einstellungen", the toolbar menu will not be restored or rather the R.menu.toolbar_menu will not inflated again. That works only correct when I swich between BottomnavigationBar Menu.

Wrong Toolbar MenuItems

save_toolbar_menu.xml

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

<item
    android:id="@+id/toolbar_nav_cancel"
    android:title="@string/title_cancel"
    app:showAsAction="always" />
<item
    android:id="@+id/toolbar_nav_save"
    android:title="@string/title_save"
    app:showAsAction="ifRoom" />
</menu>

toolbar_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/toolbar_nav_fav"
    android:icon="@drawable/ic_favorite_white"
    android:title="@string/title_fav"
    app:showAsAction="ifRoom" />
</menu>

app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/main_content" />
</android.support.design.widget.CoordinatorLayout>

main_content.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/app_bar_main">

<FrameLayout
    android:id="@+id/main_content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true" />

</RelativeLayout>

fragment_home.xml

<android.support.constraint.ConstraintLayout
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"
tools:context=".HomeFragment">
<FrameLayout
    android:id="@+id/home_frame"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="@+id/home_navigation"
    app:layout_constraintBottom_toTopOf="@+id/home_navigation"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<android.support.design.widget.BottomNavigationView
    android:id="@+id/home_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="@color/colorPrimary"
    app:itemBackground="@color/colorPrimary"
    app:itemIconTint="@color/bottom_nav_colors"
    app:itemTextColor="@color/bottom_nav_colors"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:menu="@menu/bottom_navigation_menu" />
</android.support.constraint.ConstraintLayout>
Erkan
  • 140
  • 2
  • 11
  • Where are you setting different menu for different fragment? share your code – Suraj Vaishnav Oct 03 '18 at 14:56
  • I've edited the post and shared the XML files. I am setting the `save_toolbar_menu.xml` inside `AddFragment.java` (like described above) and the `toolbar_menu.xml`in the `MainActivity.java` – Erkan Oct 03 '18 at 15:31

0 Answers0