Enabling Hierarchical Navigation

So far, CriminalIntent relies heavily on the Back button to navigate around the app. Using the Back button is temporal navigation. It takes you to where you were last. Hierarchical navigation, on the other hand, takes you up the app hierarchy. (It is sometimes called ancestral navigation.)

In hierarchical navigation, the user navigates up by pressing the Up button on the left side of the toolbar.

Enable hierarchical navigation in CriminalIntent by adding a parentActivityName attribute in the AndroidManifest.xml file.

Listing 13.11  Turning on the Up button (AndroidManifest.xml)

<activity
    android:name=".CrimePagerActivity"
    android:parentActivityName=".CrimeListActivity">
</activity>

Run the app and create a new crime. Notice the Up button in the crime detail screen, as shown in Figure 13.11. Pressing the Up button will take you up one level in CriminalIntent’s hierarchy to CrimeListActivity.

Figure 13.11  CrimePagerActivity’s Up button

Screenshot shows CriminalIntent banner with a back arrow placed on the left.

How hierarchical navigation works

In CriminalIntent, navigating with the Back button and navigating with the Up button perform the same task. Pressing either of those from within the CrimePagerActivity will take the user back to the CrimeListActivity. Even though they accomplish the same result, behind the scenes they are doing very different things. This is important because, depending on the application, navigating up may pop the user back multiple activities in the back stack.

When the user navigates up from CrimePagerActivity, an intent like the following is created:

Intent intent = new Intent(this, CrimeListActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();

FLAG_ACTIVITY_CLEAR_TOP tells Android to look for an existing instance of the activity in the stack, and, if there is one, pop every other activity off the stack so that the activity being started will be top-most (Figure 13.12).

Figure 13.12  FLAG_ACTIVITY_CLEAR_TOP at work

Figure shows FLAG_ACTIVITY_CLEAR_TOP at work.
..................Content has been hidden....................

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