Reputation: 5362
I am trying to build a menu navigation system throughout my Activities. I'll use a couple of Activities in my application as an example. I have a Maps Activity, Login and List Activity.
Maps Activity can navigate to these activities after pressing the physical menu button: Login and List. A map icon/button will not appear in the menu because it is already in the Maps Activity.
Likewise, List can go to Login and Maps but not itself. The same with Login, it can go to Map and List but not itself.
Right now, I have a separate menu layout file for each and every activity which contains basically the same thing but for example, the Maps Activity will not contain the maps menu item.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/list" android:title="List" android:icon="@drawable/ic_menu_list"/>
<item android:id="@+id/login" android:title="Login" android:icon="@drawable/ic_menu_login"/>
</menu>
and then List will be something like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/mapViewList" android:title="Map View" android:icon="@drawable/ic_menu_map"/>
<item android:id="@+id/loginList" android:title="Login" android:icon="@drawable/ic_menu_login"/>
</menu>
Therefore, my question is, can I use one single menu layout file to contain every menu item, even if some activities do not use that item? Is it safe to have multiple activities pointing to the same menu file?
I.e. Is this ok? Bearing in mind that not all items will be used in an activity such as the refresh item, which is shown only in Maps.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/add" android:title="Add" android:icon="@drawable/ic_menu_add"/>
<item android:id="@+id/mapView" android:title="Map View" android:icon="@drawable/ic_menu_map"/>
<item android:id="@+id/list" android:title="List" android:icon="@drawable/ic_menu_list"/>
<item android:id="@+id/refresh" android:title="Refresh" android:icon="@drawable/ic_menu_refresh"/>
<item android:id="@+id/login" android:title="Login" android:icon="@drawable/ic_menu_login"/>
<item android:id="@+id/logout" android:title="Logout" android:icon="@drawable/ic_menu_logout2"/>
</menu>
Upvotes: 0
Views: 1383
Reputation: 82543
Yes, it is same to have multiple Activities use the same menu XML.
Keep in mind that all options will be shown to the user, even if you don't use some of them. You can make them do nothing when clicked, but they will still be visible.
You can override onPrepareOptionsMenu() to hide some of them though, for example the map one:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.mapView).setVisible(false);
return true;
}
Upvotes: 1