Open vinothbabu opened 11 years ago
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...
Thanks for the reply, the onCreateOptionsMenu
does get called in SherlockListFragment but not in ListFragment
class. the method onCreateOptionsMenu
does get called at all inside a class which extends the Fragment where we want to show a different menu for each fragments.
Update:
I changed over to SherlockFragment and SherlockListFragment, the onCreateOptionsMenu
is getting called..
@vinothbabu what is yout example code resolved? Thanks
extend SherlockFragment instead of Fragment,Now onCreateOptionsMenu is called in each fragment
@Madhanj It quite confused me!
onCreateOptionsMenu method does not get called in Fragment class.
Answer: I imported both the library ABS and sliding menu, and extended SherlockFragment in each Fragment classes. this way i achieved calling onCreateOptionsMenu
For example: public class Fragment1 extends SherlockFragment{
@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { // TODO Auto-generated method stub super.onCreateOptionsMenu(menu, inflater);//method is called } }
@Madhanj Thx a lot! onCreateOptionsMenu
was called successfully~
but, is there somebody tell me why it can't when extended Fragment rather than SherlockFragment class?
i think we can't by extending Fragment. Because in sliding menu library they imported com.actionbarsherlock.app.SherlockFragmentActivity and used as Super class. You can go through their library and check the below class.
public class SlidingFragmentActivity extends SherlockFragmentActivity implements SlidingActivityBase {
}
I also verified: " extend SherlockFragment instead of Fragment,Now onCreateOptionsMenu is called in each fragment " I think this point should be specially documented. It almost spends me half of a day to figure it out.
@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."
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.