Actionbar should not show up button on tablet


#1

I noticed when running the app on tablet, the actionbar still shows the Up/Home button, pressing it would cause the app to crash obviously. Just letting you guys know about it in case you want to fix it in future prints.


#2

Yes, I just hit this. The reason is that in the manifest, the CrimeListActivity does not declare a parent activity, which makes NavUtils throw an exception. What is the resolution to this? Is it a special declaration in the manifest file or special run-time checking or both?

FWIW I fixed the problem by adding a check to onOptionsItemSelected in CrimeFragment

[code] public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Activity a = getActivity();

            // Navigate back if possible, otherwise exit.
        	if (NavUtils.getParentActivityName(a) != null) {
        		NavUtils.navigateUpFromSameTask(getActivity());
        	} else {
        		a.finish();
        	}
            ...
}[/code]

#3

Good catch! I’ll add it to the errata.

My preferred correction is this, inside onCreateView:

    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_crime, parent, false);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB &&
        		NavUtils.getParentActivityIntent(getActivity()) != null) {
            getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
        }

#4

This code already appears in Listing 16.13 (p. 266 in print).