Search code examples
androidandroid-toolbar

toolbar menu item click in fragments


I know this question has been asked and but I am unable to solve my problem after looking at those answer. I have an Activity with a Fragment. In fragment, I have added the toolbar and I want to handle toolbar menu item click events from the fragments.

In menu, I have added a single share button. I am getting the click event callback for the Up navigation (arrow home button) but I am not getting click event callback for the share button in my fragment.

Can some points me at what I am doing wrong here.

menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools" 
     tools:context="com.rahul.haridasasampada.details.DescriptionActivity">
     <item
         android:id="@+id/action_share"
         android:title="Share"
         android:orderInCategory="100"
         app:showAsAction="always"           
         app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>
 </menu> 

I have added toolbar to the fragment layout. My Activity's code -

public class DescriptionActivity extends ActionBarActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_description);

        if (savedInstanceState == null) {
            Article articleExtra = (Article) getIntent().getParcelableExtra(DescriptionFragment.ARGS_EXTRA);
            DescriptionFragment descriptionFragment = DescriptionFragment.newInstance(articleExtra);
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, descriptionFragment)
                    .commit();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getSupportActionBar().setTitle(R.string.app_name_in_kannada);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_description, menu);
        Log.d("debug", "activity : onCreateOptionsMenu");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                Log.d("debug","activity: action home has clicked");
                onBackPressed();
                return false;
            case R.id.action_share:
                Log.d("debug","activity: action share has clicked");
                return false;
        }

        return super.onOptionsItemSelected(item);
    }


}

My Fragments code -

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    // some code
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ActionBarActivity actionBarActivity = (ActionBarActivity) getActivity();
    Toolbar toolbar = (Toolbar) getView().findViewById(R.id.toolbar_actionbar);
    actionBarActivity.setSupportActionBar(toolbar);
    toolbar.setOnMenuItemClickListener(this);
    toolbar.inflateMenu(R.menu.menu_description);
}

@Override
public boolean onMenuItemClick(MenuItem menuItem) {
    switch (menuItem.getItemId()) {
        case R.id.action_share:
            if (menuItem.getItemId() == R.id.action_share)
                Log.d("debug","action share has clicked");
            return true;
    }
    return false;
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    Log.d("debug", "fragment : onCreateOptionsMenu");
    MenuItem shareMenuItem = menu.findItem(R.id.action_share);
    mShareActionProvider =(ShareActionProvider)MenuItemCompat.getActionProvider(shareMenuItem);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case android.R.id.home:
            Log.d("debug","fragment : action home has clicked");
            return true;
        case R.id.action_share:
            Log.d("debug","fragment: action share has clicked");
            return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View detailFragmentView = inflater.inflate(R.layout.fragment_description, null);

    return detailFragmentView;
}

Solution

  • From Android Docs, for Fragment:

    public void onCreateOptionsMenu (Menu menu, MenuInflater inflater)

    Initialize the contents of the Activity's standard options menu. You should place your menu items in to menu. For this method to be called, you must have first called setHasOptionsMenu(boolean).

    so you want to control actionbar menu in Fragment, then you have to call setHasOptionsMenu method better in Fragment's onCreate(...):

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }
    

    also important is that you should not use inflateMenu and onCreateOptionsMenu both together for same ToolBar, inflateMenu is for standalone without setting ToolBar as ActionBar.


    Suggestion:

    keep one ToolBar in Activity as ActionBar for your App, and another ToolBar standalone inside Fragment.

    Hope this help!