Chapter 17 Two-Pane Master-Detail Interfaces
Run CriminalIntent on a tablet. Create a new crime, and a CrimeFragment will be added and shown in
the detail_fragment_container. Then view an old crime to see the CrimeFragment being swapped
out for a new one (Figure 17.7).
Figure 17.7 Master and detail now wired up
Looks great! One small problem, though: If you make changes to a crime, the list will not update
to reflect them. Right now, you only reload the list immediately after adding a crime and in
CrimeListFragment.onResume(). But on a tablet, CrimeListFragment stays visible alongside the
CrimeFragment. The CrimeListFragment is not paused when the CrimeFragment appears, so it is
never resumed. Thus, the list is not reloaded.
You can fix this problem with another callback interface – this one in CrimeFragment.
Implementing CrimeFragment.Callbacks
CrimeFragment will define the following interface:
public interface Callbacks {
void onCrimeUpdated(Crime crime);
}
For CrimeFragment to push updates to a peer Fragment, it will need to do two things. First,
since CriminalIntent’s single source of truth is its SQLite database, it will need to save its Crime
to CrimeLab. Then CrimeFragment will call onCrimeUpdated(Crime) on its hosting activity.
CrimeListActivity will implement onCrimeUpdated(Crime) to reload CrimeListFragment’s list,
which will pull the latest data from the database and display it.