PopupMenu with checkableBehavior="single" group ignores setCheck() on items

963 Views Asked by At

I have a PopupMenu define in XML below being part of a Fragment

<menu xmlns:com.itrash.android="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

<group android:id="@+id/order_by_menu" android:checkableBehavior="single">
   <item android:id="@+id/action_show_discounts_only"
         android:title="@string/discounts_only_title" />

   <item android:id="@+id/action_show_all"
         android:title="@string/all_title" />      
</group>

The menu is linked to ImageButton (snippet from onActionCreated() )

        final SharedPreferences prefs = getActivity().getPreferences(Context.MODE_PRIVATE);        
    this.onlyDiscounts = prefs.getBoolean(PROPERTY_ONLY_DISCOUNTS, false);
    this.orderBy = prefs.getString(PROPERTY_ORDER_BY, ORDER_BY_NAME); 


this.filterButton = (ImageButton) getActivity().findViewById(R.id.filter_button);       
    this.filterButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            filterMenu.show();              
        }
    });                
    this.filterMenu = new PopupMenu(getActivity(), getActivity().findViewById(R.id.filter_button));
    this.filterMenu.inflate(R.menu.product_filter_menu);
    this.filterMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            onFilterClick(item);
            return true;
        }
    });

When clicked the following method is called

private void onFilterClick(MenuItem item) {
    boolean showDiscountsOnly = item.getItemId() == R.id.action_show_discounts_only ? true : false;

    item.setChecked(!item.isChecked());             

    if (this.onlyDiscounts != showDiscountsOnly) {
        this.onlyDiscounts = showDiscountsOnly;

        productSearchResultAdapter.clear();

        this.curPage = 1;

        doSearchProducts();
    }

    final SharedPreferences prefs = getActivity().getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putBoolean(PROPERTY_ONLY_DISCOUNTS, showDiscountsOnly);
    editor.commit();
}

Problem I am facing is how to restore state of menu items according to attribute onlyDiscounts when retrieved from SharedPreferences? In other words I want to have the menu item *action_show_discounts_only* checked if onlyDiscounts is true.

I have tried to place inside onActionCreated() something along the lines

        for (int i = 0; i < this.filterMenu.getMenu().size(); i++) {
        MenuItem item = this.filterMenu.getMenu().getItem(i);
        if (item.getItemId() == R.id.action_show_discounts_only) {
            item.setChecked(true);
        } else {
            item.setChecked(false);
        }
    }

or even

this.filterMenu.getMenu().findItem(R.id.action_show_discounts_only).setChecked(this.onlyDiscounts);

this.filterMenu.getMenu().findItem(R.id.action_show_all).setChecked(!this.onlyDiscounts);

Both attempts were ignored, *R.id.action_show_all* item is always check no matter what value onlyDiscounts is.

Any idea how to resolve this?

0

There are 0 best solutions below