Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9209511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:51:37+00:00 2026-06-18T00:51:37+00:00

I have a FragmentActivity class with a 2-options menú inherited from a superclass for

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:51:39+00:00Added an answer on June 18, 2026 at 12:51 am

    Ok, so I made it work already. It seems I was missing some code in the Activity, namely this:

    Here I inflate the menu

        @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.menu_detalle_tapa, (Menu) menu);
        MenuItem item = menu.findItem(R.id.ab_comparte);
        mShareActionProvider = (ShareActionProvider) item.getActionProvider();
    
        this.menu = menu;
        return true;
    }
    

    This bit happens when I already got the remote data (so you cannot try to share nulls/empty objects

                /** Getting the target intent */
            Intent intent = getDefaultShareIntent();
            Log.d(CData.LOGTAG, "check if intent is null ");
            /** Setting a share intent */
            if(intent!=null)
                mShareActionProvider.setShareIntent(intent);
            else
                Log.d(CData.LOGTAG, "intent was null ");
    

    And here is where I decide what’s to be shared

    private Intent getDefaultShareIntent(){
    
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        //intent.putExtra(Intent.EXTRA_SUBJECT, "SUBJECT");
        String en = Tappabook.getAppContext().getResources().getString(R.string.en);
        String comparteTapa = 
                tapaDetallada.getNombre() + " " 
                        + " " + en + " " 
                        + tapaDetallada.getBarNombre() + " "
                        + CData.urlCompartirTapa + tapaDetallada.getId()
                        ;
        intent.putExtra(Intent.EXTRA_TEXT,comparteTapa);
        return intent;
    }
    

    Turns out I was being a moron 😛

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class that extend FragmentActivity in it I add fragment to layout as
So I have a simple Activity with following code: public class MainActivity extends FragmentActivity
I have a FragmentActivity this activity has a Fragment this Fragment consist from widgets
I have a fragment where I wish to call a method from the FragmentActivity
have written this little class, which generates a UUID every time an object of
I have A fragment in another class and i want to launch it from
I have a FragmentActivity (main) which creates 3 Fragments and also a menu. Pretty
this problem this driving me crazy.... I have a FragmentActivity. Inside I have a
I have a FragmentActivity where I have written code to create custom title bar
i have a problem to pass extras between an Activity and a FragmentActivity. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.