Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1

How hierarchical navigation works


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


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



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


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

Free download pdf