Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: menu item click event from fragment

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

like image 974
user3503093 Avatar asked Jan 31 '26 08:01

user3503093


1 Answers

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

like image 109
mromer Avatar answered Feb 01 '26 22:02

mromer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!