不同片段的不同菜单 [英] Different menu for different fragments

查看:99
本文介绍了不同片段的不同菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的活动有2个片段.
一次可以看到一个片段,每个片段都有一个不同的选项菜单.

I have an activity which has 2 fragments.
1 fragment is visible at a time and each fragment has a different option menu.

我可以通过2种不同的方式来实现此行为.

I can achieve this behavior by 2 different ways.

1-我可以通过在每个伴奏中调用onCreateOptionsMenu为每个片段添加不同的菜单.
2-我在活动级别只能有一个菜单,并且可以选择显示onPrepareOptionsMenu

1 - I can add different menu for each fragment by calling onCreateOptionsMenu in each friengment.
2 - I can have only one menu at activity level and can select to show particular option in onPrepareOptionsMenu

我想知道的是:
哪种方法更可实现此功能?
推荐什么?

What I want to know is:
Which is the preferable way to implement this functionality?
What is recommended?

推荐答案

我将遵循第一个选项,因为为每个片段提供专用的资源菜单看起来更加简洁,并且还降低了维护可见内容所需的代码复杂性而不是什么(如果您要通过onPrepareOptionsMenu并具有隐藏和显示不同菜单的代码).

I would follow the first option as having a dedicated resource menu for each fragment seems cleaner and also reduces the code complexity you would have in order to maintain what is visible and what is not (if you would go through onPrepareOptionsMenu and have code to hide & show different menus).

如果您的片段中有一些操作,则可以创建一个基础片段类,每个片段都将从中扩展.

If you have some actions in your fragments, then you could create a base fragment class that each of your fragments would extend from.

这篇关于不同片段的不同菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆