Re-using Options menu code

前端 未结 3 633
盖世英雄少女心
盖世英雄少女心 2021-01-05 16:51

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.

    <
3条回答
  •  长情又很酷
    2021-01-05 17:33

    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.

提交回复
热议问题