title: onOptionsItemSelected listener not working
body: <p>I'm developing an android app and I have an issue with the options menu. The onOptionsItemSelected() method is not called when the menu item is pressed, I'm using an actionLayout so, as many questions posted in this site, I've set my own listener. Nothing seems to work and I can't see how my solution differs from others.</p>
  
  <p>Here it's my code:</p>
  
  <blockquote>
    <p>On MainActivity.java</p>
  </blockquote>
  
  <pre><code>@Override
  public boolean onCreateOptionsMenu(Menu menu) {
  
      Log.d("Listener", "Created");
      getMenuInflater().inflate(R.menu.main, menu);
      for (int i = 0; i &lt; menu.size(); i++) {
          final MenuItem item = menu.getItem(i);
          if (item.getItemId() == R.id.action_cart) {
              View itemChooser = item.getActionView();
              if (itemChooser != null) {
                  itemChooser.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View v) {
                          Log.d("Listener", "Clicked1");
                          onOptionsItemSelected(item);
                      }
                  });
              }
          }
      }
      return super.onCreateOptionsMenu(menu);
  }
  
   @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      switch (item.getItemId()) {
          case R.id.action_cart:
              Log.d("Listener", "Clicked");
              CartFragment newFragment = new CartFragment();
              FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  
              // Replace whatever is in the fragment_container view with this fragment,
              // and add the transaction to the back stack so the user can navigate back
              transaction.replace(R.id.fragment_container, newFragment);
              transaction.addToBackStack(null);
              // Commit the transaction
              transaction.commit();
              return true;
          default:
              return super.onOptionsItemSelected(item);
      }
  
  }
  </code></pre>
  
  <blockquote>
    <p>On menu/main.xml</p>
  </blockquote>
  
  <pre><code>&lt;menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"&gt;
  &lt;item
      android:title="cart"
      android:id="@+id/action_cart"
      app:showAsAction="always"
      app:actionLayout="@layout/badge_layout"/&gt;
  
  &lt;/menu&gt;
  </code></pre>
  
  <blockquote>
    <p>On layout/badge_layout.xml</p>
  </blockquote>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:clickable="true"
  android:focusable="true"
  android:addStatesFromChildren="true"&gt;
  
  &lt;com.joanzapata.iconify.widget.IconButton
      android:layout_width="44dp"
      android:layout_height="44dp"
      android:textSize="24sp"
      android:textColor="#ffffff"
      android:background="@mipmap/ic_cart"
      android:id="@+id/badge_icon_button"/&gt;
  
  &lt;TextView
      android:id="@+id/badge_textView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignTop="@id/badge_icon_button"
      android:layout_alignLeft="@id/badge_icon_button"
      android:layout_alignStart="@id/badge_icon_button"
      android:text="10"
      android:paddingEnd="12dp"
      android:paddingRight="0dp"
      android:paddingLeft="0dp"
      android:gravity="center"
      android:textColor="#ffffff"
      android:textSize="13sp"
      android:textStyle="bold"
      android:background="@drawable/badge_circle"/&gt;
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p>As for the logs, "Listener: Created" is the only one printed.</p>
  
  <p>I've already tried the solutions of several related questions like <a href="https://stackoverflow.com/questions/20931812/onoptionsitemselected-not-called-when-clicking-on-menu-item-which-has-an-actio">this one</a> and <a href="https://stackoverflow.com/questions/11627892/onoptionsitemselected-not-called-when-using-actionlayout-sherlockactionbar">this one</a>. The actionlayout represents a badge inside a shopping cart and I know it has something to do with that since before the actionlayout it worked.</p>
  
  <p>Any help is appreciated. Thanks.</p>