android - Android 操作栏的最佳实践

标签 android android-fragments android-activity android-actionbar

我是 so 的新手,也是 android 开发的新手,并且已经养成了一些坏习惯。

基本上我已经编写了一个应用程序,使用旧的学习资源作为引用,但没有涵盖 fragment 。因此,我有几个标准 Activity 正在转换为 fragment 。

我无法找到有关操作栏最佳实践的一件事;最好在 Activity 中或 fragment 中定义操作栏?

我注意到调用 onCreateOptionsMenu(Menu menu, MenuInflater inflater) 会导致在 Activity 中调用相同方法的细微变化,是否可以就最好在哪里调用它以及它提供什么好处提供建议?

谢谢

最佳答案

是的,这个方法对于 fragment 有点不同:

void onCreateOptionsMenu(Menu menu, MenuInflater inflater)

和 Activity :

boolean onCreateOptionsMenu(Menu menu)

逻辑非常简单:如果您要在所有 fragment 中使用相同的操作栏(相同数量的操作),最好在 Activity 中定义它。如果 Action Bar 布局依赖于 fragment ,最好在那里定义它。

关于android - Android 操作栏的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21284209/

相关文章:

android - 在 fragmentHolder 中切换 fragment 后 ListView 清空

android - 如何在 Fragment 中创建一个 MapView

java - JNI 错误 : accessed stale weak global reference

android - 如何在 Android 应用程序中使用 iptables

android - 带有 EditText 的回收器 View 适配器

android - 究竟何时调用 onSaveInstanceState() 和 onRestoreInstanceState()?

android - 使用 Fragment Tabs 将 Model View Presenter 模式应用到 Android

android - 为什么在按下后退按钮后没有为 Activity 调用onResume(),有什么好的解决方法?

android - 未在 ListView 中调用子类化的 RelativeLayout onDraw() 方法

android - SQLite .query() 方法,WHERE 子句只采用双引号字符串