Reusing Options Menu Code

Is there a convenient way to display the same options menu options in multiple actions?

Example. In my application, I show the TV Guide in one of three ways.

  • Seven-Day Reference (7-Tab TabActivity)
  • All Show Channels (ListActivity)
  • Everything shows today by start time (Activity - can easily be changed to ListActivity)

In the Options menu in TabActivity code is pretty 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 to me that I need to copy / paste it into the other two actions that I don't like. If I changed the code for the Options menu for one, I would need to do this for the other two as well.

The only alternative I can come up with is a “helper class” (POJO), to which I could add a method, and pass context inside to allow the use of the getMenuInflator() method and another method that I could pass the result of item.getItemId() for processing using the patch case.

What is the normal way to have multiple activities with the same options menu?

+6
source share
3 answers

One approach is to use inheritance with your actions. Create a basic activity that implements the options menu methods, and then each of the child actions will receive this functionality. This is the recommended approach on the Android developer site:

Tip. If your application contains several actions, and some of them provide the same options menu, consider creating an action that does not implement anything except the onCreateOptionsMenu () and onOptionsItemSelected () methods. Then extend this class for each action that should have the same options menu. Thus, you only need to manage one set of code to handle actions in the menu, and each descendant class inherits the menu behavior.

Unfortunately, this will not work for you, since you do not inherit from the activity itself, but differ in subclasses of it, but this is the usual way to do it.

+5
source

Create a simple separate class using 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 actions, override these callback methods and redirect the call to an instance of the 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 allow you to keep in one place code that responds to the selection of your main menu items, so there will be no need to worry about copying it into all the actions that should have the same menu.

+10
source

You can encapsulate your action menu in fragment . Thus, you only need to add a fragment to the onCreate menu of your activity.

After creating the fragment, call setHasOptionsMenu . To add an add fragment, use the tag instead of the layout identifier.

+2
source

Source: https://habr.com/ru/post/901409/


All Articles