In my app I'm having a class which extends SherlockFragmentActivity. Within that Activity, I'm loading four classes which extends SherlockFragment which pretends as Tabs with the help of ActionbarSherlock and PagerSlidingTabStrip.
The classes which extends SherlockFragment has setHasOptionsMenu(true); inside onCreate() method. Even having that, when run the application, it won't call onCreateOptionsMenu() method.
I have read these similar questions (Q1, Q2) in SO and didn't get any help.
What can be the reason for that?
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.record, menu);
super.onCreateOptionsMenu(menu, inflater);
Log.i(TAG, "* onCreateOptionsMenu");
}
UPDATE:
I think the reason is, I'm using SherlockFragment inside another SherlockFragment. Means in PageSlidingTabStripFragment.java has following method inside MyPagerAdapter class.
@Override
public SherlockFragment getItem(int position) {
return SuperAwesomeCardFragment.newInstance(position);
}
What I'm doing is, I'm calling some other SherlockFragments from there like below.
@Override
public SherlockFragment getItem(int position) {
Log.i(TAG, "* getItem");
switch (position) {
case 0:
return new InnerSherlockFragmentOne();
case 1:
return new InnerSherlockFragmentTwo();
case 2:
return new InnerSherlockFragmentThree();
case 3:
return new InnerSherlockFragmentFour();
default:
return null;
}
}
What is not calling is onCreateOptionsMenu(Menu menu, MenuInflater inflater) inside those InnerSherLockFragments.
menu.clear()
before inflating the menu. – GlowwormonCreateOptionsMenu()
not even call. If I put aLog
message inside that method as first line, it won't print. – BrewisMenuItems
programmatically. But to createMenuItems
programatically we need to fireonCreateOptionsMenu()
inside Fragment. That is the place not happening to me. You may have implemented theMenuItems
insideSherlockFragmentActivity
notSherlockFragment
OR you may not creating differentMenuItems
for different Fragments. By the way, thanks for sharing your experience. – Brewis