Items not saving across invocations


The app is running just fine: I can add, delete, etc.

However, I noticed that when I exit the app, the results are not being saved after deletions. That is, I delete some crime, then hit the return key and further delete the CriminalIntent app from the task list. Next time I start up, the deleted item is back! I put a breakpoint in the onPause method, but noticed that it is not called when I remove CriminalIntent from the task list. That means that the save operation doesn’t happen.

Why is the onPause method not called in this case?

Answer: Since the delete is happening in the same activity.

BUT, see below…


Digging into this further brings up another question:

since adapter.notify… is called, shouldn’t we somewhere be calling the registerDataSetObserver method? Otherwise it seems that the delete operation is not written to the file, since nothing is set up to capture the event. OTOH for new crimes, we have a separate intent so capture the changes in onPause when the intent finishes. FWIW, I counted three calls to saveCrimes during an add (or just looking at a crime, even if nothing was changed – why?) and none during a delete.

Is something missing or am I going crazy here?


1- There’s no need to hook up a DataSetObserver. This interface is used to update the ListView; ListView has set one itself, so notifyDataSetChanged() has the effect of updating the list.

2- You’re not crazy - this code is missing. You can either call saveCrimes() immediately after deleting a crime, or do what our solutions do - put a call to saveCrimes() within deleteCrime().


I actually have this observation but i easily implemented the onPause on the CrimeListFragment that’s it.

The onPause that the book says is implemented on the CrimeFragment only so if you are on the detail view and you exist the onPause got called and your CrimeLab saved.
But when you deleted something and return to the CrimeListFragment, if you exit the onPause got called obviously but you didn’t implement it on that fragment.

I hope this can help clarify something. Cheers.