这个方法做什么super.onCreateOptionMenu()和super.onOptionsItemSelected(item) [英] What does this method do super.onCreateOptionMenu() and super.onOptionsItemSelected(item)

查看:149
本文介绍了这个方法做什么super.onCreateOptionMenu()和super.onOptionsItemSelected(item)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android新手.我知道以前曾问过这个问题,但我仍然感到困惑.当在我的onCreateOptionMenu()和onOptionItemSelected()内部将它们返回时,此方法将做什么?

任何人都可以帮助我带来什么影响

1)如果我返回true

2)如果我返回false

3)当我返回super.onCreateOptionMenu()和super.onOptionItemSelected时会发生什么

任何人都可以用一个很好的例子向我解释一下.我仍然感到困惑.

解决方案

好,让我们首先看看您感兴趣的两种方法

  @Overridepublic boolean onCreateOptionsMenu(Menu menu){getMenuInflater().inflate(R.menu.menu_main,menu);返回true;} 

如果返回true == >>>,则表示您想查看已膨胀的选项菜单.如果返回false == >>>,您不想显示它

  @Overridepublic boolean onOptionsItemSelected(MenuItem item){int id = item.getItemId();如果(id == R.id.action_settings){返回true;}//激活导航抽屉开关如果(mDrawerToggle.onOptionsItemSelected(item)){返回true;}返回super.onOptionsItemSelected(item);} 

根据文档true->事件已消耗,不应转发给其他事件false->转发给其他人食用

当我们使用多个片段并且每个片段都有自己的Option菜单和OnOptionItemSelected的Overrideing(主要在平板电脑设计中)时,这种布尔返回类型实际上会受益

在这种情况下,android会跟踪每个片段的OnOptionItemSelected方法,以免发生这种情况

a)如果onOptionsItemSelected()中有任何片段正在消耗事件,则返回"true",否则返回"false"

b)如果我们返回false,则它将跟踪其他连接的片段(onOptionsItemSelected)方法,直到结束所有片段或有人将其消耗为止.

您的第三个答案就是克里希纳(KrishnaJ)写的

super.onCreateOptionMenu() super.onOptionItemSelected

如果您编写此代码,则它将在扩展该类中的任何类时首先调用您的父类.如果方法也位于父类中,它将作为父类工作.

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.

解决方案

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.

这篇关于这个方法做什么super.onCreateOptionMenu()和super.onOptionsItemSelected(item)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆