I'm using ActionBarSherlock in my project and sometimes need to add one or more items inside the action bar.
At this BaixadosFragment class (that extends SherlockFragment), I'm using the following code and it works fine:
@Override
public void onCreateOptionsMenu(Menu menu,MenuInflater inflater)
{
inflater.inflate(R.menu.main, menu);
super.onCreateOptionsMenu(menu, inflater);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh:
refresh();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
In this case, I'm adding a refresh button, witch is lonely inside main.xml
BUT I want to do the same at CupomDetalheActivity (though adding a share button), witch extends SherlockFragmentActivity instead. So I'm not able to override "onCreateOptionsMenu" as it has a different signature (below):
//this is inside SherlockFragmentActivity
public final boolean onCreateOptionsMenu(android.view.Menu menu) {
return true;
}
//this is inside SherlockFragment
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//Nothing to see here.
}
Whith SherlockFragmentActivity, I don't even see where can I use the inflater to bring up the xml containing the share button... I appreciate a lot any ideas and suggestions...
[EDIT] This worked, according to DroidT's suggestion:
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.share, menu);
super.onCreateOptionsMenu(menu);
return true;
}
Your SherlockFragmentActivity also has an
onCreateOptionsMenu()
andonPrepareOptionsMenu()
. You can inflate your menu options in theonCreateOptionsMenu()
by usinggetSupportMenuInflater()
. You would want to make a call toinvalidateOptionsMenu()
in your SherlockFragmentActivity whenever you want the change to happen and add the menu options inonPrepareOptionsMenu()
. For more information look at the "Changing menu items at runtime" section of this link.If you are using a menu inside of a fragment, make sure you call
setHasOptionsMenu(true);
in the fragmentsonCreate(Bundle savedInstance)
method