What does this method do super.onCreateOptionMenu() and super.onOptionsItemSelected(item)

杀马特。学长 韩版系。学妹 提交于 2020-06-23 09:55:12

问题


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.


回答1:


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.




回答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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/38887304/what-does-this-method-do-super-oncreateoptionmenu-and-super-onoptionsitemselec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!