Delete challenge from previous chapter broken on tablet

I completed the delete challenge in chapter 13. Works fine on my phone, not on a tablet. I read other solutions from previous revisions. I understand I simply can’t call getActivity().finish() from my onOptionsItemSelected() within CrimeFragment.java. I think my problem is a little different though…when I’m in tablet mode and I press the delete button from the detail fragment side I end up in onOptionsItemSelected() in CrimeListFragment.java and not in CrimeFragment.java.

This seems to differ from what others are experiencing as others still seem to be able to modify onOptionsItemSelected() in CrimeFragment.java…I can catch the delete case in CrimeListFragment.java, but I have no “mCrime” to work with. Am I making an inflation mistake somewhere? Did something change from previous revisions where I actually am in the correct function and the solution this revision is different? Thanks.

When you select a menu option, the system will call onOptionsItemSelected() on the activity, followed by all attached fragments in the order in which they were added. If any of these return true, the menu item has been handled, and this process stops.

Since you do not want to handle the delete option in CrimeListFragment, make sure onOptionsItemSelected() returns false in this case (via the superclass implementation).

That was it. Thank you.

In case anyone else was wondering(I hope this is correct)…from there I let my onCrimeDeleted callbacks do the work. The CrimePagerActivity.java version just called finish()(for non tablets this is ok). Then the CrimeListActivity callback version displayed the first crime in the detail screen or if the list was empty made the layout visibility “GONE”(just set it again to “VISIBLE” when you select a crime to cover the corner case of deleting everything and attempting to re-add crimes.

The only thing that I’m weird about is my default case catching the up toolbar navigation for non tablets. I noticed allowing the default case to cause a stack overflow was very helpful in catching unexpected selections as from my first post in this thread.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.delete_crime:
            CrimeLab.get(getActivity()).deleteCrime(mCrime.getId());
            //If we're in a "detail" fragment, we have to update the list side
            //of the tablet.
            Activity activity = getActivity();
            if (activity.findViewById(R.id.detail_fragment_container) != null) {
                updateCrime();
            }
            //Let the callback function in the respected fragment handle what to
            //do from here.
            mCallbacks.onCrimeDeleted();
            return true;
        default:
            //This handles the case for phones only since if we get here then
            //we've use the toolbar "up" navigation. Our CrimePagerActivity(for phones)
            //will simply call finish().
            mCallbacks.onCrimeDeleted();
            return true;
    }
}