0 votes
ago in Education by (1.7m points)
(This post isn't helpful)

I'm using this code in onCreateOptionsMenu to enable/disable menu items in a fragment:

@Override

    public void onCreateOptionsMenu(Menu menu,MenuInflater inflater) {

        inflater.inflate(R.menu.menu_main,menu);

        this.menu = menu;

        inflater.inflate(R.menu.menu_main, menu);

        assert ((AppCompatActivity)getActivity()).getSupportActionBar() != null;

        ((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(inEditMode);

        menu.findItem(R.id.signOut).setVisible(!inEditMode);

        menu.findItem(R.id.edit).setVisible(inEditMode);

        menu.findItem(R.id.delete).setVisible(inEditMode);

        menu.findItem(R.id.info).setVisible(inEditMode);

    }

However, even when inEditMode is false, all of the Menu Items are visible. This is inside a fragment, which I am using via ViewPager. Here's my AppCompatActivity's 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:id="@+id/activity_main"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="com.mastermindcorp.alibdeir.salawat.MainActivity">

<android.support.design.widget.AppBarLayout

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:theme="@style/ThemeOverlay.AppCompat.Light">

    <android.support.v7.widget.Toolbar

        android:id="@+id/toolbar"

        android:layout_width="match_parent"

        android:layout_height="?attr/actionBarSize"

        android:background="?attr/colorPrimary"

        app:layout_scrollFlags="scroll|enterAlways"

        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"

        app:titleTextColor="@android:color/white" />

    <android.support.design.widget.TabLayout

        android:id="@+id/tabs"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        app:tabGravity="fill"

        app:tabMaxWidth="0dp"

        app:tabMode="fixed"

        app:tabTextColor="@android:color/white"

        app:tabIndicatorColor="?attr/colorControlNormal" />

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

<android.support.v4.view.ViewPager

    android:id="@+id/viewpager"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

I'm not setting a Toolbar in my Fragment; I only have my Toolbar in the Activity. Should I set my Toolbar in every Fragment? If not, why is this issue occuring and how can I fix it?

This worked before I converted my AppCompatActivity to Fragment

JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
ago by (1.7m points)
It turns out I had to add:

super.onCreateOptionsMenu(menu,inflater);

Not sure why this is needed in Fragments but not in Activitys
...