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:

  1. 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 the SetHasOptionsMenu() method.

  2. 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.com/apk/res-auto">
      <item android:id="@+id/action_share"
        android:icon="@android:drawable/ic_menu_share"
        yourapp:showAsAction="ifRoom"
        android:title="@string/action_share" />
    </menu>
  3. And finally inflate it in the OnCreateOptionsMenu() method of the fragment:
    public override void OnCreateOptionsMenu(
      IMenu menu, MenuInflater inflater) {
        inflater.Inflate(Resource.Menu.ActionBarItems, menu);
      }

Another thing we may want to do is to make the home button in the action bar and change to an up button when we navigate down to a fragment:

  1. To do this, we add logic to show the up arrow when there are items on the fragment back stack:
    private void OnBackStackChanged() {
      bool hasBack = SupportFragmentManager.BackStackEntryCount > 0;
      SupportActionBar.SetDisplayHomeAsUpEnabled(hasBack);
    }
  2. And then, we call that method to update the home button when the back stack changes, also making sure to call it when the activity starts:
    protected override void OnCreate(Bundle bundle) {
      ...
      SupportFragmentManager.BackStackChanged += delegate {
        OnBackStackChanged();
      };
      OnBackStackChanged();
    }
  3. Finally, we override the home button press so that we can pop the fragments:
    public override bool OnSupportNavigateUp() {
      SupportFragmentManager.PopBackStack();
      return true;
    }

How it works...

If a fragment wants to place items on the action bar, we have to let the activity know. The way we do this is by setting the HasOptionsMenu property to true, or on Android versions 3.0 and above, by passing true to the SetHasOptionsMenu() method.

We can then go ahead and create and inflate the menu structure in the same manner as if we were adding an options menu to an activity. There is an OnCreateOptionsMenu() method in the fragment that performs the same function as the method on the activity.

Another useful thing for us to do is to make the home button into an up button after we navigate to a child fragment. This is simple to add by subscribing to the BackStackChanged event on the fragment manager. In this event, we can check to see if there is anything on the back stack and update the home button.

Note

The home button should be refreshed in the OnCreate() method as the activity may have just been restored from a saved state, such as after a device orientation change.

After adding the up arrow to the home button, we can override the OnSupportNavigateUp() method or the OnNavigateUp() method to pop the back stack of the fragment manager.

See also

  • Navigating between fragments
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset