24
@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();

    //no inspection 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 it didn' t got called, if i add in MainActivity, it works but i want to call it in Fragment. How can i do this ?

Ichigo Kurosaki
  • 3,765
  • 8
  • 41
  • 56
Nevermore
  • 1,663
  • 7
  • 30
  • 56

4 Answers4

72

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;
    }
N J
  • 27,217
  • 13
  • 76
  • 96
12

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.

Ichigo Kurosaki
  • 3,765
  • 8
  • 41
  • 56
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;
    }
}
Aashish Kaushik
  • 450
  • 4
  • 17
0

Ok, many answers and none of the above shows how to actually call Frarment.

This is the whole example tested and working.

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //activate menu on the top to show icons or menu
        supportActionBar!!.setDisplayHomeAsUpEnabled(true)


        //this part we need to show back arrow on top so user could use backstack
        setupActionBarWithNavController(findNavController(R.id.fragmentContainer))
        //setupActionBarWithNavController(findNavController(R.id.fragmentContainer))

    }

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.fragmentContainer)
        return navController.navigateUp() || super.onSupportNavigateUp()
    }


    //show icons in top menu
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        //inflate our menu our menu situated in res menu menu.xml so R.menu.menu(our menu)
        menuInflater.inflate(R.menu.menu, menu)
        return true
    }

    //if item in menu selected do ......
    override fun onOptionsItemSelected(item: MenuItem): Boolean {

        val navController = findNavController(R.id.fragmentContainer)
        when (item.itemId) {
            **R.id.iconAbout -> navController.navigate(R.id.aboutFragment)**

        }
        return super.onOptionsItemSelected(item)
    }
}
Valery Lavrov
  • 91
  • 1
  • 3