Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Xamarin Mobile Development for Android Cookbook

You're reading from   Xamarin Mobile Development for Android Cookbook Over 80 hands-on recipes to unleash full potential for Xamarin in development and monetization of feature-packed, real-world Android apps

Arrow left icon
Product type Paperback
Published in Nov 2015
Publisher
ISBN-13 9781784398576
Length 456 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Matthew Leibowitz Matthew Leibowitz
Author Profile Icon Matthew Leibowitz
Matthew Leibowitz
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Working with Xamarin.Android FREE CHAPTER 2. Showing Views and Handling Fragments 3. Managing App Data 4. Presenting App Data 5. Communicating with the Outside World 6. Using Background Tasks 7. Notifying Users 8. Interacting with Other Apps 9. Presenting Multimedia 10. Responding to the User 11. Connecting to Wearables 12. Adding In-App Billing 13. Publishing Apps Index

Navigating with the action bar

The action bar is used to allow the user to navigate to a parent activity, as well as show the user where they are in the app.

How to do it...

Navigation with the action bar is an upward navigation, rather than a backward navigation. This navigation is very simple to add and involves only two steps. If we support versions of Android versions below 4.1, we will make use of the support library.

  1. First, we need to ensure that our source activity is accessible using a known name by adding a [Register] attribute:
    [Register("com.xamarincookbook.MainActivity")]
    public class MainActivity : AppCompatActivity
    {
    }
  2. Next, we let the system know which activity we want to navigate up to using a [MetaData] attribute:
    [MetaData(
      "android.support.PARENT_ACTIVITY", 
      Value = "com.xamarincookbook.MainActivity")]
    public class RecipeDetailsActivity : AppCompatActivity {
    }
  3. Then in the child activity, we let the action bar know that we want to allow upward navigation:
    SupportActionBar.SetDisplayHomeAsUpEnabled(true);

If the Android version is 4.1 and above, we use the native types and members:

  1. First, we set the ParentActivity property on the [Activity] attribute:
    [Activity (ParentActivity = typeof(MainActivity))]
    public class RecipeDetailsActivity : Activity
    {
    }
  2. Then, we let the child activity's action bar know that we want to allow upward navigation:
    ActionBar.SetDisplayHomeAsUpEnabled(true);

How it works...

The action bar can facilitate direct navigation in two ways: navigating up to the parent activity and navigating down to a child activity. Navigating down is often done by adding action items to the action bar.

Action bar automatically navigates up to the parent activity when the user taps the icon, which is different from the traditional back navigation. The up navigation within an app is based on the hierarchical relationships between activities, that is, navigation to the parent activity. The back navigation is navigation back through the history of activities, in reverse chronological order.

Tip

If an activity is the topmost one in an app and it does not have a parent activity, it should not present an up button.

Sometimes the back navigation is the same as the up navigation. This happens when the previously viewed screen is also the hierarchical parent of the current screen. However the up navigation will keep the user in the app, but back navigation may return the user to the home screen or another app.

When adding the [MetaData] attribute to the activity, we need to reference the final compiled name of the parent activity. Xamarin.Android mangles the final name of the types to avoid possible conflicts, so we have to let the compiler know exactly what name to use. We do this using a [Register] attribute on the parent activity, and we then use the same value for the value component of the metadata.

The action bar lets the user know where they are in the app by using the action bar's title, which is usually the current activity's label. This can be customized by assigning a new string value to the Title property on the ActionBar instance.

There's more...

Sometimes the up navigation will take the user to different parent activities, depending on how the user arrived at the current activity. In these cases, we override several members in our activity. If our app is not going to have the activity instantiated on any other apps, we only need to override the SupportParentActivityIntent or ParentActivityIntent properties:

public override Intent SupportParentActivityIntent {
  get {return new Intent(this, typeof(MainActivity));}
}

If our activity is going to be used by other apps, we also need to override the OnCreateNavigateUpTaskStack() or OnCreateSupportNavigateUpTaskStack() method.

See also

  • The Adding an action bar recipe
You have been reading a chapter from
Xamarin Mobile Development for Android Cookbook
Published in: Nov 2015
Publisher:
ISBN-13: 9781784398576
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image