How to handle onContextItemSelected in a multi fragment activity?

前端 未结 11 2075
天命终不由人
天命终不由人 2020-11-29 17:26

I\'m currently trying to adapt my application to use the \"Compatibility Libraries for Android v4\" to provide the benefits of the usage of fragments even to Android 1.6 use

11条回答
  •  粉色の甜心
    2020-11-29 18:04

    I found out a very easy solution. As onCreateContextMenu() is called every time the ContextMenu is created I set a boolean variable to true.

    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getActivity().getMenuInflater();
        inflater.inflate(R.menu.film_menu, menu);
        bMenu=true;
    }
    

    The only other thing I have to do is ask for that variable OnContextItemSelected()

    public boolean onContextItemSelected(MenuItem item) {
        if (bMenu) {
            bMenu=false;
            if (item.getItemId() == R.id.filmProperties) {
                ///Your code
                return true;
            } else {
                return super.onContextItemSelected(item);
            }
        } else {
            return super.onContextItemSelected(item);
        }
    }
    

    That's it.

提交回复
热议问题