OptionMenu为整个应用程序在Android中?
我可以创建一个选项菜单的一个活动。但现在我想创造一个应该适用于所有活动中的应用程序菜单中的退出选项。是否有创建一个选项菜单,一旦某个应用程序的可能的方式是什么?
I can create an Option Menu for a single Activity. But now I want to create a Logout option in the menu which should be available on all Activities in the App. Is there a possible way to create an Option menu once for an Application?
我在SO回答此一次,但我忘了在哪里......所以在这里我又来了。
I answered this once on SO, but i forgot where...so here i go again.
它非常简单。
创建一个 BaseActivity
(扩展活动
)。实现所有的选项菜单code本。重写你的 onOptionsMenu
等等...
Create a BaseActivity
(extends Activity
). Implement all your Options Menu Code in this. Override your onOptionsMenu
,etc...
现在,为了你的所有其他活动,而不是延长活性,延长 BaseActivity
。你最终会得出所有的选项菜单code ......漂亮吧?
Now, For all your other Activities, instead of extending Activity, extend BaseActivity
. You'll end up deriving all the options menu code...nifty eh?
code一次,多次使用...
Code once, use multiple times...
OOP为赢!
编辑:WarrenFaith指出的那样,你需要创建 BaseListActivity
和 BaseMapActivity
如果你使用 ListActivity
和 MapActivity
。
As WarrenFaith points out, you'll need to create BaseListActivity
and BaseMapActivity
if you're using ListActivity
and MapActivity
.