Re-using Options menu code

我们两清 提交于 2019-11-30 05:14:10

问题


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.

  1. Seven day guide (TabActivity with 7 tabs)
  2. All channels 'Now showing' (ListActivity)
  3. All shows today by start time (Activity - could be changed easily to ListActivity)

For the Options menu in the TabActivity, the code is quite simple...

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    menu.clear();
    inflater.inflate(R.menu.gv_options_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.view:
        ...
    ...
    }
}

...but at the moment it seems I need to copy/paste it to the other two Activities which I don't like doing. If I change the Options menu code for one I'll need to do it for the other two also.

The only alternative I can think of is I have a 'helper' class (POJO) to which I could add a method and pass the context into to allow use of the getMenuInflator() method and another method I could pass the result of item.getItemId() into to process with the switch-case.

What is the normal way of having multiple Activities with the same Options menu?


回答1:


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.




回答2:


Create a simple separate class with these two methods:

public class MyMenuHandler {

    private Activity mActivity;

    public MyMenuHandler(Activity activity) {
        mActivity = activity;
    }

    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuInflater inflater = mActivity.getMenuInflater();
        menu.clear();
        inflater.inflate(R.menu.gv_options_menu, menu);
        return true;
    }

    public boolean onOptionsItemSelected(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:

public class MyActivity1 extends TabActivity {

    private MyMenuHandler mMenuHandler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        ...
        mMenuHandler = new MyMenuHandler(this);
    }

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

    @Override
    public boolean onOptionsItemSelected(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.




回答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.



来源:https://stackoverflow.com/questions/8116588/re-using-options-menu-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!