Deleting a Crime - Detail View Still Shows It


#1

The detail view of a Crime remains in the right pane after it gets deleted in the listview.

I fixed this by adding this to both the places in CrimeListFragment where a Crime could be deleted:

And then modifying CrimeListActivity.onCrimeSelected() to display a blank detail view:

[code]public void onCrimeSelected(Crime crime) {
if (findViewById(R.id.detailFragmentContainer) == null) {

} else {
// Tablet
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();

	Fragment oldDetail = fm.findFragmentById(R.id.detailFragmentContainer);

	if (oldDetail != null) {
		ft.remove(oldDetail);
		ft.commit();
	}

	if (crime != null) {
		Fragment newDetail = CrimeFragment.newInstance(crime.getId());
		ft.add(R.id.detailFragmentContainer, newDetail);
		ft.commit();
	}
}

}[/code]

It works. Is it a good way to do this?

Also, is it necessary to do the callback when something in the detail view is updated (photo or suspect) that is not displayed in the listview?

Thanks.


#2

1- That’s a great solution. The only fix I can see is that only one commit should be necessary:

      if (oldDetail != null) {
         ft.remove(oldDetail);
      }

      if (crime != null) {
         Fragment newDetail = CrimeFragment.newInstance(crime.getId());
         ft.add(R.id.detailFragmentContainer, newDetail);
      }

      ft.commit();

2- From a design perspective, yes. CrimeFragment does not know which fields CrimeListFragment is displaying, after all - it must assume that it might be displaying any of them.


#3

I thought of doing it with the one commit at the end, but both changes are in IF statements, and you could end up doing a commit when nothing changed. Maybe the best solution (if the commits are expensive and should be done once) is to set a flag in those IF statements and then do the one commit of it is ON.

Thanks.


#4

Yeah, but you’re doing two commits now when the fragment changes. I’m surprised to know that it works, but apparently it does.