androidandroid-contextmenu

Is super.onCreateContextMenu() required?


I have an ActionBarActivity( from v7 support library) which loads a fragment( from the v4 support library). I implemented onCreateContextMenu() in the fragment as shown below:

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
                                ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_menu, menu);
}

In my attempt to understand if a call to the super method was actually required, I dug through following the android source code:

  1. Fragment, which has one line of code:

    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        getActivity().onCreateContextMenu(menu, v, menuInfo);
    }
    
  2. ActionBarActivity, which does not have onCreateContextMenu().

  3. As ActionBarActivity is a subclass of FragmentActivity, I looked at here, which also does not have onCreateContextMenu().

  4. As FragmentActivity is a subclass of Activity, I looked at here. Its method is empty:

    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    }
    

I concluded that there is no point in calling the super method. Yet, the official documentation does call super. So is my conclusion wrong? If so, why?


Solution

  • Your conclusion is right, the documentation just has a superflous line of code there, that does no harm.

    Other methods like onCreate(), onResume() etc.however, do require a call to the method in the parent class and throw runtime exceptions if they are not called.