Style Question


#1

The book has this for the final onOptionsItemSelected:

@TargetApi(11)
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_item_new_crime:
                ...
                return true;
            case R.id.menu_item_show_subtitle:
                if (getActivity().getActionBar().getSubtitle() == null) {
                    getActivity().getActionBar().setSubtitle(R.string.subtitle);
                    mSubtitleVisible = true;
                    item.setTitle(R.string.hide_subtitle);
                }
                else {
                    getActivity().getActionBar().setSubtitle(null);
                    mSubtitleVisible = false;
                    item.setTitle(R.string.show_subtitle);
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

I noticed the repeated calls to getActivity().getActionBar() and wondered if it is considered good form to rephrase the second “case” like this:

case R.id.menu_item_show_subtitle:
			ActionBar actionBar = getActivity().getActionBar();
			if (actionBar.getSubtitle() == null) {
				actionBar.setSubtitle(R.string.subtitle);
				mSubtitleVisible = true;
			} else {
				actionBar.setSubtitle(null);
				mSubtitleVisible = false;
			}
			return true;

#2

I did the same thing. However, don’t forget to set your item title. Otherwise the code is equivalent (and easier to read).


#3

Yes, I think that’s a definite improvement.