DeepakR February 2016

Unable to use OptionsMenu in Fragment Activity

I want to use options menu inside Fragment Activity .I have used the following code to add option menu:

1.menu_chat.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/action_viewContacts"
        android:orderInCategory="100"
        android:title="@string/action_view_contacts"
        app:showAsAction="never"/>
    <item android:id="@+id/action_media"
        android:orderInCategory="100"
        android:title="@string/action_media"
        app:showAsAction ="never"/>
    <item android:id="@+id/action_search_message"
        android:orderInCategory="100"
        android:title="@string/action_search"
        app:showAsAction ="never"/>
    <item android:id="@+id/action_block"
        android:orderInCategory="100"
        android:title="@string/action_block"
        app:showAsAction ="never"/>

    <item android:id="@+id/action_email_chat"
        android:orderInCategory="100"
        android:title="@string/action_email_chat"
        app:showAsAction ="never"/>

    <item android:id="@+id/action_clear_chat"
        android:orderInCategory="100"
        android:title="@string/action_clear_chat"
        app:showAsAction ="never"/>
    <item android:id="@+id/action_attach"
        android:orderInCategory="100"
        android:title="@string/action_clear_chat"
        android:icon="@drawable/attach"
        app:showAsAction ="always"/>

</menu>

2.Code of FragmentActivity

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    Log.e("Option Menu","called");
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_chat_activity, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch        

Answers


Stanojkovic February 2016

"Double-check that you have really set the app theme to Holo.Light. I can't explain what is causing the problem, but I was able to reproduce it. After experimenting with changes between FragmentActivity and AppCompatActivity, I accidentally ran with FragmentActivity and theme set to Theme.AppCompat.Light. This created the behavior you are seeing, with onCreateOptionsMenu() not called."

As a side issue, you should fix your onCreateOptionsMenu() to call through to super as noted in the documentation: "Deriving classes should always call through to the base implementation".

Originaly answered HERE

Post Status

Asked in February 2016
Viewed 1,417 times
Voted 4
Answered 1 times

Search




Leave an answer