Add onOptionsItemSelected calling in Fragment

19,302

Solution 1

In Fragment you have to call setHasOptionsMenu(true)

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

Then suppose you have to handle menu_item_to_handle_in_fragment item click

For Fragment class

  @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

        case R.id.menu_item_to_handle_in_fragment:
            // Do onlick on menu action here
            return true;
        }
    return false;
}

For Activity class

 @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {

            case R.id.menu_item_to_handle_in_fragment:
                return false;
            }
        return false;
    }

Solution 2

You need to add setHasOptionMenu(true) in your onCreate of fragment.
When you add this option the fragment lifecycle will calls the onCreateOptionMenu() and onOptionItemSelected().

Follow this steps:

  • Add setHasOptionsMenu(true) method in onCreate() of your Fragment.

  • Override onCreateOptionsMenu(Menu menu, MenuInflater inflater) and onOptionsItemSelected(MenuItem item) methods in your Fragment.

  • Inside your onOptionsItemSelected(MenuItem item) Activity's method, make sure you return false when the menu item action would be implemented in onOptionsItemSelected(MenuItem item) Fragment's method.

Solution 3

Steps to create Option Menu in fragment

1.

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_home, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

2.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case android.R.id.home:
            //call function as per your requirement
            return true;
        default:
            return false;
    }
}
Share:
19,302

Related videos on Youtube

Nevermore
Author by

Nevermore

Updated on September 15, 2022

Comments

  • Nevermore
    Nevermore over 1 year
    @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.
        int id = item.getItemId();
    
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_filter) {
            FragmentManager fm = getSupportFragmentManager();
            if (userType.equals("İş Arayan"))
                filterDialogTitle = "İş İlanları Filtre";
            else if (userType.equals("Hizmet Arayan"))
                filterDialogTitle = "Hizmet İlanları Filtre";
            FilterDialogFragment editNameDialogFragment = FilterDialogFragment.newInstance(filterDialogTitle);
            editNameDialogFragment.show(fm, "fragment_edit_name");
            return true;
        }
    
        return super.onOptionsItemSelected(item);
    }
    

    I added in Fragment, but i didn' t called, if i add in MainActivity, it works but i want call in Fragment. How can i do this ?

  • Azlan Jamal
    Azlan Jamal about 5 years
    what if implementation need to be done in both activity and fragment?
  • MontDeska
    MontDeska almost 4 years
    Don't forget to add "setHasOptionsMenu(true);" in onCreate()
  • Roland
    Roland over 3 years
    @AzlanJamal feel free to just override this event in both activity and fragment
  • Roland
    Roland over 3 years
    setHasOptionsMenu(true) also works great in the (kotlin) handler onViewCreated. +1