How to add an option menu to a clip on Android

I'm trying to add an item to the options menu from a set of clips.

I created a new MenuFragment class and extended it to the fragment I want to include the menu item in. This is the code:

public class MenuFragment extends Fragment {

    MenuItem fav;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        fav = menu.add("add");
        fav.setIcon(R.drawable.btn_star_big_off);
    }
}

For some reason, onCreateOptionsMenu doesn't seem to work.

#1 building

If you find that the onCreateOptionsMenu(Menu menu, MenuInflater inflater) method is not invoked, please ensure that the following methods are invoked from the onCreate(Bundle savedInstanceState) method of Fragment:

setHasOptionsMenu(true)

#2 building

I have the same problem. My clip is the page of ViewPager. The reason for this is that when I instantiate the fragmentpager adapter, I use the sub fragment manager instead of the active support fragment manager.

#3 building

I have the same problem, but I think it's best to summarize and introduce the last step to make it work:

  1. Add the setHasOptionsMenu (true onCreate(Bundle savedInstanceState) method to the onCreate(Bundle savedInstanceState) method of the fragment.

  2. Override the oncreateoptionsmenu (menu menu, menuinflator inflater) and onoptionsiteselected (menu item item) methods in Fragment.

  3. Within the method of the optionsitemselected (MenuItem item) activity, make sure that the menu item operation returns false when it will be implemented in the method of the optionsitemselected (MenuItem item) fragment.

An example:

activity

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case R.id.activity_menu_item:

            // Do Activity menu item stuff here
            return true;

        case R.id.fragment_menu_item:

            // Not implemented here
            return false;
        default:
            break;
    }

    return false;
}

subsection

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    ....
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // Do something that differs the Activity's menu here
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case R.id.activity_menu_item:

            // Not implemented here
            return false;
        case R.id.fragment_menu_item:

            // Do Fragment menu item stuff here
            return true;

        default:
            break;
    }

    return false;
}

#4 building

If you need menu to refresh webview within a specific Fragment, you can use:

Fragment:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    // TODO Add your menu entries here
    inflater.inflate(R.menu.menu, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.exit:
        System.exit(1);
        break;

    case R.id.refresh:
        webView.reload();
        break;
    }
    return true;

}

menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/exit" android:title="Exit" android:icon="@drawable/ic_action_cancel" />
    <item android:id="@+id/refresh" android:title="Refresh" android:icon="@drawable/ic_action_refresh" />
</menu>

#5 building

Setting the options menu after creating the clip view works well for me.

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setHasOptionsMenu(true);        
}

Tags: Fragment Android xml

Posted on Thu, 13 Feb 2020 08:21:11 -0800 by horseygirl