question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

onCreateOptionsMenu method does not get called in Fragment class.

See original GitHub issue

onCreateOptionsMenu method does not get called, even after invaliding the menu.

getSlidingMenu().setOnOpenedListener(new OnOpenedListener() { @Override public void onOpened() { supportInvalidateOptionsMenu(); Log.d("SLIDING DRAWER","OPENED"); } });

Called the above inside the BaseActivity onCreate() and also the FragmentClass which extends the BaseActivity method.

The Fragment class onCreate() method has setHasOptionsMenu(true);. Still it does not trigger or invoke the onCreateOptionsMenu() method.

Issue Analytics

  • State:open
  • Created 10 years ago
  • Comments:10

github_iconTop GitHub Comments

1reaction
tiran133commented, Mar 28, 2013

I did the same in my project and it works fine. I extend my fragment as follow SlidingMenuListFragment extends SherlockListFragment I’m using the sherlockactionbar So i extend SherlockListFragment and not just ListFragment. Then I have the following code in my SlidingMenuListFragment .

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    this.mMenu = menu;

    if (menu != null) {
        mAdapter = new SlidingMenuListAdapter(getActivity());
        for (int i = 0; i < menu.size() - 1; i++) {
            MenuItem tmpitem = menu.getItem(i);
            mAdapter.add(tmpitem);
        }

        setListAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();

    }
}

This works fine for me… As a result I can add menu entries in the xml file displayd in the behindview from the slidingMenu. Hope that helps. In addition I added the following attribute to certain menu entries in the xml file android:visible=“false” because I don’t want them to show up in the actual menu…

0reactions
MohammadRaficommented, Apr 6, 2015

@faywong : here is the link which documents this feature http://actionbarsherlock.com/usage.html.

which reads “NOTE: When using SherlockFragmentActivity there are also ‘Sherlock’-prefixed fragment classes which you should use to ensure proper functionality (e.g. SherlockFragment, SherlockListFragment). The activity will still function with the normal fragment classes but you will not be able to use any of the menu-related methods.”

Read more comments on GitHub >

github_iconTop Results From Across the Web

OnCreateOptionsMenu() not called in Fragment - Stack Overflow
In your fragment's onCreateView() method, you need to call setHasOptionsMenu(true) to tell the host activity that your fragment has menu ...
Read more >
No warning when not your Activity does not call super in Menu ...
When updated the androidx fragment from 1.4.0 to 1.5.0 the onCreateOptionsMenu is not called anymore. SetHasOptionsMenu(true) is called so the ...
Read more >
Fragment | Android Developers
Returns the Transition that will be used to move Views out of the scene when the fragment is removed, hidden, or detached when...
Read more >
Creating and Using Fragments | CodePath Android Cliffnotes
onActivityCreated() is called when host activity has completed its onCreate() method. onStart() is called once the fragment is ready to be displayed on...
Read more >
Android - onCreateOptionsMenu is deprecated - YouTube
Links:GitHub repo ▻ https://github.com/MrBean355/youtube- android /tree/main/options-menu- ... Your browser can't play this video.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found