2

I am new to android. I know this question have been asked before but, i am still confuse . What this method does when returning them inside my onCreateOptionMenu() and onOptionItemSelected()

Can any one help me what effect i will have

1)if i return true

2)if i return false

3)What will happen when i return super.onCreateOptionMenu() and super.onOptionItemSelected

Can anyone please explain me this with good example. I am still confuse.

FaisalAhmed
  • 3,469
  • 7
  • 46
  • 76

4 Answers4

5

Ok, let's first see the two methods of your interest

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

if return true ==>>> It means you want to see the option menu which you have inflated. if return false ==>>> you do not want to show it

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }
    // Activate the navigation drawer toggle
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

As per documentation true --> Event Consumed now It should not be forwarded for other event false --> Forward for other to consume

This Boolean return type actually benefits when we are working with multiple fragments and every fragment has their own Option menu and Overriding of OnOptionItemSelected(Mainly in tablet design)

In this case android trace every fragments OnOptionItemSelected method so to avoid that

a) If any fragment is consuming event in onOptionsItemSelected() so return "true" else return "false"

b) If We return false then It will trace other connected fragment's (onOptionsItemSelected) method until it ends all fragment or Somebody consumes It.

And your 3rd answer is as KrishnaJ written

super.onCreateOptionMenu() and super.onOptionItemSelected

If you write this then It will first call your parent class this method If you extend any class in this class.It will work as parent class if Methods are in parent class too.

Abdullah
  • 935
  • 9
  • 18
  • when i use getMenuInflater().inflate(R.menu.menu, menu); return true; and when i click menu button from mobile , one more copy of menu items appear . Then i used menu.clear() and its works fine . so should i always do menu.clear ... see here http://stackoverflow.com/questions/38869893/menu-items-keeps-on-adding-on-every-menu-button-press – FaisalAhmed Aug 11 '16 at 06:26
  • yes it's ok . This is how android is designed, lets say on performing some actions if some icons should change in your actions bar... so the whole view should get refreshed and oncreteoptions menu will get recalled – Abdullah Aug 11 '16 at 06:34
2

Ok. I got you question:

Que 1 :

What this method does when returning them inside my onCreateOptionMenu() and onOptionItemSelected()

Ans :

onCreateOptionMenu() used for inflate menu in action bar.

onOptionItemSelected() used for capture onclick of that menus

Que 2 :

if i return true or false

Ans :

ref !!! you should return true if you have inflated menu in that file or your defined menu is clicked. else u should return false. so compiler will find for men or menu item in other page.

eg. you have one activity and two fragment, then if menu or menu item not find in activity then compiler will find it in fragment. if you return true then no further search.

Que 3 :

why to use super ?

Ans :

so compiler get to know that in this file there is no user defined menu or item value.

Community
  • 1
  • 1
Dharvik shah
  • 1,969
  • 12
  • 27
  • when i use getMenuInflater().inflate(R.menu.menu, menu); return true; and when i click menu button from mobile , one more copy of menu items appear . Then i used menu.clear() and its works fine . so should i always do menu.clear ... see here http://stackoverflow.com/questions/38869893/menu-items-keeps-on-adding-on-every-menu-button-press – FaisalAhmed Aug 11 '16 at 05:16
  • I see that in your question, both time you return super. if you use return true and not initiate menu again from their fragment then no menu will duplicate. I add answer for your question here : http://stackoverflow.com/a/38889193/5432730 – Dharvik shah Aug 11 '16 at 06:56
0

onCreateOptionMenu() method used to create an option menu.

onOptionsItemSelected() method used to handling the event of option menu i.e. which menu action is triggered and what should be the outcome of that action etc.

Dnyanesh
  • 331
  • 3
  • 12
0

I like to add your 3rd question answer in answer of Dnyanesh

super.onCreateOptionMenu() and super.onOptionItemSelected

If you write this then It will first call your parent class this method If you extend any class in this class.It will work as parent class if Methods are in parent class too.

Community
  • 1
  • 1
Krishna Kachhela
  • 773
  • 1
  • 7
  • 24