Skip to content Skip to sidebar Skip to footer

Re-using Options Menu Code

Is there a convenient way of showing the same Options menu options in multiple Activities? Example: In my app, I display a TV Guide in one of three ways. Seven day guide (TabActiv

Solution 1:

Create a simple separate class with these two methods:

publicclassMyMenuHandler {

    private Activity mActivity;

    publicMyMenuHandler(Activity activity) {
        mActivity = activity;
    }

    publicbooleanonPrepareOptionsMenu(Menu menu) {
        MenuInflaterinflater= mActivity.getMenuInflater();
        menu.clear();
        inflater.inflate(R.menu.gv_options_menu, menu);
        returntrue;
    }

    publicbooleanonOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.view:
            ...
        }
    }
}

In your activities override those callback methods and redirect the call to an instance of your MyMenuHandler class:

publicclassMyActivity1extendsTabActivity {

    privateMyMenuHandler mMenuHandler;

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        ...
        mMenuHandler = newMyMenuHandler(this);
    }

    @OverridepublicbooleanonPrepareOptionsMenu(Menu menu) {
        // you may also add here some items which are specific // for one activity, not for the others
        ...
        return mMenuHandler.onPrepareOptionsMenu(menu);
    }

    @OverridepublicbooleanonOptionsItemSelected(MenuItem item) {
        // handle selection of your specific items here,// if none of them has been selected call mMenuHandler method
        ...
        return mMenuHandler.onOptionsItemSelected(item);
    }
}

This will let you hold in one place the code which respond to selection of your basic menu items, so there will be no need to worry about copy-pasting it to all activities which are to have the same menu.

Solution 2:

One approach is to use inheritance with your Activities. Create a base Activity that implements the options menu methods and then each child Activity will gain that functionality. This is the recommended approach on the Android developer site:

Tip: If your application contains multiple activities and some of them provide the same Options Menu, consider creating an activity that implements nothing except the onCreateOptionsMenu() and onOptionsItemSelected() methods. Then extend this class for each activity that should share the same Options Menu. This way, you have to manage only one set of code for handling menu actions and each descendant class inherits the menu behaviors.

Unfortunately this won't work for you as you are not inheriting from Activity itself but differing subclasses of it, but that is the 'normal' way to do it.

Solution 3:

You can encapsulate your action menu in a fragment. In this way you only need to add the fragment in the onCreate menu of your activity.

You need to call setHasOptionsMenu once the fragment is created. To add the add fragment use a tag instead of a layout id.

Post a Comment for "Re-using Options Menu Code"