I have a FragmentActivity class with a 2-options menú inherited from a superclass for code-reusing purposes.
In this FragmentActivity I add a third menu item and set the onOptionsItemSelected to do what I want for the Share menu Item (the third one gets its own onMenuItemClick listener). However, when I run the app, 2nd and 3rd options (one that leads to menu and the new one) do register clicks and do what they’re told, but the 2nd option (one set to call the share intent) does not. Tapping it does nothing.
this is my menu xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/ab_comparte"
android:icon="@android:drawable/ic_menu_share"
android:showAsAction="always|withText"
android:title="@string/menu_share"
android:menuCategory="container"
android:orderInCategory="1"
android:actionProviderClass="com.actionbarsherlock.widget.ShareActionProvider"
/>
<item
android:id="@+id/ab_menu"
android:icon="@drawable/ic_action_bar_menu"
android:showAsAction="always|withText"
android:title="@string/menu_menu"
android:menuCategory="container"
android:orderInCategory="2"/>
</menu>
this is the Fragment Activity (only menu-related bits)
public boolean onCreateOptionsMenu(Menu menu) {
/*
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu_detalle_tapa, (Menu) menu);
*/
menu.add("Me Gusta")
.setOnMenuItemClickListener(this)
.setIcon(R.drawable.like_blanco)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return super.onCreateOptionsMenu(menu);
}
.....
@Override
public boolean onMenuItemClick(MenuItem item) {
Crouton.makeText(this, "ME GUSTA", Style.ALERT).show();
return false;
}
...................
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
Log.d(CData.LOGTAG, "pulsado en menú item " + itemId);
if (itemId == R.id.ab_comparte){
// cerrar sesión
Log.d(CData.LOGTAG, "pulsado en menú item Comparte" + itemId);
Crouton.makeText(this, "ahora se abriría el menú de compartir ", Style.INFO).show();
Intent i=new Intent(android.content.Intent.ACTION_SEND);
i.setType("text/plain");
/*i.putExtra(Intent.EXTRA_SUBJECT,
Tappabook.getAppContext().getResources().getString(R.string.asunto_compartir_tapa)
);*/
String en = Tappabook.getAppContext().getResources().getString(R.string.en);
String comparteTapa =
tapaDetallada.getNombre() + " "
+ " " + en + " "
+ tapaDetallada.getBarNombre() + " "
+ CData.urlCompartirTapa + tapaDetallada.getId()
;
i.putExtra(Intent.EXTRA_TEXT,comparteTapa);
startActivity(
Intent.createChooser(
i,
Tappabook.getAppContext().
getResources().getString(R.string.titulo_compartir_tapa)
)
);
return true;
}else{
Log.d(CData.LOGTAG, "pulsado en menú item " + itemId);
return super.onOptionsItemSelected(item);
}
}
Here’s the superclass menu-related bit
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu_activity_login, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.ab_menu){
// cerrar sesión
Log.d(CData.LOGTAG, "pulsado en menú item Menú" + itemId);
Intent intent = new Intent(this, MenuFA.class);
//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
}else{
Log.d(CData.LOGTAG, "pulsado en menú item " + itemId);
return super.onOptionsItemSelected(item);
}
}
So what am I doing wrong? option ab_menu works as it should. option “Me gusta” (with its own listener) also works. But option ab_comparte, though it appears on the actionbar, does not even ‘listen’ to the click.
Any help will be appreciated.
UPDATE:
I’ve changed this on the xml of the share item
android:showAsAction="collapseActionView|always"
Now, when I click on the share menu item, the actionbar changes and it shows another item with a (different) share icon. If I click on this second share item it does work, it calls the Share Intent. However, I’d like not to have to do this. Just clicking the first item should call the Share Intent, not change the actionbar to show a second share item…. What am I doing wrong?
Ok, so I made it work already. It seems I was missing some code in the Activity, namely this:
Here I inflate the menu
This bit happens when I already got the remote data (so you cannot try to share nulls/empty objects
And here is where I decide what’s to be shared
Turns out I was being a moron 😛