官术网_书友最值得收藏!

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
主站蜘蛛池模板: 修武县| 南安市| 临湘市| 松潘县| 尉氏县| 西青区| 新泰市| 京山县| 布尔津县| 贡嘎县| 平乡县| 镇平县| 交口县| 景德镇市| 绵阳市| 上林县| 若尔盖县| 紫云| 磐石市| 西峡县| 邛崃市| 思茅市| 宣恩县| 通山县| 诏安县| 和静县| 道真| 腾冲县| 呈贡县| 抚宁县| 达孜县| 盐城市| 黄骅市| 绵竹市| 安丘市| 麻江县| 富源县| 锡林浩特市| 尼玛县| 应城市| 金坛市|