问题描述
我有两个活动都包含以编程方式使用menu.add(INT,INT,INT的CharSequence)建立了一个相同的上下文菜单。它们都使用onCreateContextMenu和
onContextItemSelected。
I have two activities that both contain an identical context menu built programmatically using menu.add(int, int, int, CharSequence). They both use onCreateContextMenu andonContextItemSelected.
第一个活动包含一个ListView,当一个项目的用户长presses出现该项目的上下文菜单。第二项活动(在活动1的每一项细节画面)包含一个按钮,当用户presses出现的上下文菜单的按钮。
The first Activity contains a ListView, when the user long presses on an item the context menu appears for that item. The second Activity (a detail screen for each item in Activity 1) contains a button, when the user presses the button the context menu appears.
我想与大家分享的code,创建这些活动之间的上下文菜单。唯一的一点这些活动占有率上下文菜单中,因此它似乎有点大材小用来创建一个超类(例如,ActivityOne扩展ContextMenuActivity),它定义onCreateContextMenu和onContextItemSelected方法。
I'd like to share the code that creates the context menu between these activities. The only thing these activities share is the context menu, so it seems like a bit of overkill to create a superclass (e.g., ActivityOne extends ContextMenuActivity) that defines onCreateContextMenu and onContextItemSelected methods.
是否有共享活动之间这些方法的更好的办法?
Is there a better way of sharing these methods between activities?
推荐答案
拓展活动覆盖了菜单的东西,那么扩展该活动的其他活动。如果您需要更改活动之间的菜单覆盖菜单的东西,在你新的活动,并呼吁超。
extend an activity overriding the menu stuff, then extend this activity for your other activities. if you need to change the menu between activities override the menu stuff in you new activities and call super.
Android开发人员指南解释了它选项菜单,但应上下文菜单和对话框工作。
android dev guide explains it for options menu but should work for context menus and dialogs.http://developer.android.com/guide/topics/ui/menus.html
这篇关于分享活动之间的上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!