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

(gtxtreme123) #1

Chapter 17  Two-Pane Master-Detail Interfaces


Next, in CrimeListActivity, implement CrimeListFragment.Callbacks. Leave
onCrimeSelected(Crime) empty for now.


Listing 17.7  Implementing callbacks (CrimeListActivity.java)


public class CrimeListActivity extends SingleFragmentActivity
implements CrimeListFragment.Callbacks {


@Override
protected Fragment createFragment() {
return new CrimeListFragment();
}


@Override
protected int getLayoutResId() {
return R.layout.activity_masterdetail;
}


@Override
public void onCrimeSelected(Crime crime) {
}
}


Eventually, CrimeListFragment will call this method in CrimeHolder.onClick(...)
and also when the user chooses to create a new crime. First, let’s figure out
CrimeListActivity.onCrimeSelected(Crime)’s implementation.


When onCrimeSelected(Crime) is called, CrimeListActivity needs to do one of two things:



  • if using the phone interface, start a new CrimePagerActivity

  • if using the tablet interface, put a CrimeFragment in detail_fragment_container


To determine which interface was inflated, you could check for a certain layout ID. But it is better to
check whether the layout has a detail_fragment_container. Checking a layout’s capabilities is a
more precise test of what you need. Filenames can change, and you do not really care what file the
layout was inflated from; you just need to know whether it has a detail_fragment_container to put
your CrimeFragment in.


If the layout does have a detail_fragment_container, then you are going to create a fragment
transaction that removes the existing CrimeFragment from detail_fragment_container (if there is
one in there) and adds the CrimeFragment that you want to see.

Free download pdf