OptionsMenu items common for multiple activites

…衆ロ難τιáo~ 提交于 2019-12-10 10:34:40

问题


The Android design guide says that Help should always be placed as the last item of the overflow menu (it should never appear in the ActionBar) and also, that is should be present in every activity, so that users don't have to look for it. Similar approach is also recommended for Settings.

However, I'm wondering what is the best way to make sure that all the activities in my app handle these items without lots of code repetition? Putting these common items manually into every XML menu file and then manually handling clicks on each of them in every activity class is just nonsense.

Since I am already extending all of my activities from a common base class (which provides some convenience methods), I came up with this approach: In the BaseActivity class, I define an empty initOptionsMenu() method which the subclasses may override (template method pattern style) by adding their specific items to the menu. This method is called at the start of onCreateOptionsMenu() and then the base class adds the common items (settings and help) at the end of the menu.

The onOptionsItemSelected() method follows the standard pattern - it switches on the item ID and in the default case, it passes the handing to the superclass. Again, the base class handles the setting and help cases.

public class BaseActivity extends Activity {

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        initOptionsMenu(menu);
        menu.add(Menu.NONE, R.id.menu_settings, Menu.NONE, R.string.menu_help);
        menu.add(Menu.NONE, R.id.menu_help, Menu.NONE, R.string.menu_help);
        return true;
    }

    protected void initOptionsMenu(Menu menu) {}

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_settings:
                startActivity(new Intent(this, SettingsActivity.class));
                return true;
            case R.id.menu_help:
                startActivity(new Intent(this, HelpActivity.class));
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

Now, whenever I create a new activity, I extend the BaseActivity. If the derived activity provides some more items for the options menu, I do not override the standard onOptionsItemSelected(), but instead I override the custom initOptionsMenu() method and populate the menu there. In onOptionsItemSelected(), I need to handle only cases for the items specific for this activity, the common ones will be handled by the super call.

public class FooActivity extends BaseActivity {

    @Override
    protected void initOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.foo, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // cases for items in R.menu.foo

            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

Is this pattern sensible? Can you come up with a better approach? Please share your thoughts.


回答1:


I might not use initOptionsMenu method. I will just call super.onCreateOptionsMenu() after adding my menu from concrete implementation. My BaseActivity will add settings and help menu so in BaseActivity:

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(Menu.NONE, R.id.menu_settings, Menu.NONE, R.string.menu_help);
    menu.add(Menu.NONE, R.id.menu_help, Menu.NONE, R.string.menu_help);
    return true;
}

and in MainActivity extends BaseActivity:

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(Menu.NONE, R.id.menu_create_dummy, Menu.NONE, R.string.menu_dummy);
    menu.add(Menu.NONE, R.id.menu_delete_dummy, Menu.NONE, R.string.menu_dummy);
    return super.onCreateOptionMenu(menu);
}


来源:https://stackoverflow.com/questions/14922228/optionsmenu-items-common-for-multiple-activites

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