android: menu item click event from fragment

一笑奈何 提交于 2019-12-07 15:07:03

问题


I have an application with actionbar. And there is menu inflated by the mainactivity. I want to intercept the click event inside the fragment but I don't know how? Can you help me please? MainActivity

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

home.xml

        <menu>
        <item
            android:id="@+id/action_refresh"
            android:title="@string/menu_refresh"/>
    </menu>

And I want to refresh the list in the fragment


回答1:


If you want to capture the click on your item, implement

public boolean onOptionsItemSelected(MenuItem item)

And then:

If your activity includes fragments, the system first calls onOptionsItemSelected() for the activity then for each fragment (in the order each fragment was added) until one returns true or all fragments have been called.

You can follow the oficial reference:

http://developer.android.com/guide/topics/ui/menus.html



来源:https://stackoverflow.com/questions/25281242/android-menu-item-click-event-from-fragment

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