Fragments and the action bar
We can access the action bar from a fragment and make customizations to aid the user on a per-fragment basis. Items can be added, and you can also customize the up navigation, keeping a consistent structure with the rest of the app.
How to do it...
When a fragment is added to the activity, we can allow that fragment to add items to the action bar. This is similar to providing items from the activity. Let's take a look at the following steps:
First, we want to add items to the action bar from the fragment, so we let the activity know we have a menu:
public override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetHasOptionsMenu(true); }
Tip
If the target Android version is below version 3.0, the
HasOptionsMenu
property is used instead of theSetHasOptionsMenu()
method.We then create the menu structure in the
menu
resource folder:<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:yourapp="http://schemas.android...