Suspect name is not saving to database

I’ve gone through the entire chapter, twice, and can’t see what I am missing. I am able to create an intent to view my contacts and I am able to select one. When I return to the CrimeFragment, onActivityResult() is called and the selected contact is returned via the data. The button text is updated as appropriate and the call to the ViewModel to save the crime fires.

However, when the observer on CrimeFragment gets the crime the suspect is no longer set. Every other change to the crime saves and persists (title, date, time, solved, requiresPolice).

I even tried uninstalling the app so the database would be recreated incase my migration was incorrect, but the problem still persists.

Below is my log trace after selecting a contact and returning to CrimeFragment. Is there a step I am missing or a particular area that I should double check?

2019-09-27 20:29:01.112 9143-9143/criminalintentbnr4k D/CI4K.CrimeFragment: onCreate(Bundle?) called
2019-09-27 20:29:01.113 9143-9143/criminalintentbnr4k D/CI4K.CrimeFragment: args bundle crime ID 461f153a-34b8-43fe-aaf4-9696fae09dd7
2019-09-27 20:29:01.113 9143-9143/criminalintentbnr4k D/CI4K.CrimeDetailVM: loadCrime() called
2019-09-27 20:29:01.114 9143-9143/criminalintentbnr4k D/CI4K.CrimeListFragment: onAttach(Context) called
2019-09-27 20:29:01.114 9143-9143/criminalintentbnr4k D/CI4K.CrimeListFragment: onCreate(Bundle?) called
2019-09-27 20:29:01.114 9143-9143/criminalintentbnr4k D/CI4K.MainActivity: onCreate(Bundle?) called
2019-09-27 20:29:01.136 9143-9143/criminalintentbnr4k D/CI4K.CrimeFragment: onCreateView() called
2019-09-27 20:29:01.157 9143-9143/criminalintentbnr4k D/CI4K.CrimeFragment: onViewCreated() called
2019-09-27 20:29:01.157 9143-9143/criminalintentbnr4k D/CI4K.CrimeFragment: onActivityCreated() called
2019-09-27 20:29:01.164 9143-9143/criminalintentbnr4k D/CI4K.MainActivity: onStart() called
2019-09-27 20:29:01.167 9143-9143/criminalintentbnr4k D/CI4K.CrimeFragment: onActivityResult(2, -1, data) called
2019-09-27 20:29:01.172 9143-9143/criminalintentbnr4k D/CI4K.CrimeFragment: got suspect "Jim"
2019-09-27 20:29:01.172 9143-9143/criminalintentbnr4k D/CI4K.CrimeDetailVM: saveCrime() called
2019-09-27 20:29:01.173 9143-9143/criminalintentbnr4k D/CI4K.MainActivity: onResume() called
2019-09-27 20:29:01.174 9143-9143/criminalintentbnr4k D/CI4K.CrimeFragment: onResume() called
2019-09-27 20:29:01.179 9143-9143/criminalintentbnr4k D/CI4K.CrimeFragment: crime observed with suspect ""

Here’s an interesting update. I am running the exact same code on two different devices. It works on one, it doesn’t on the other.

  • Non-working device: physical Nexus 6 running Android 7.1.1 (API 25) with many contacts
  • Working device: emulated Nexus 6 running Android 7.0 (API 24) with 1 contact

I then tried it on additional devices

  • Working device: emulated Pixel 2 running Android 9.0 (API 28) with 1 contact
  • Working device: physical NVidia Shield running Android 7.0 (API 24) with many contacts

After seeing it worked on physical and emulated devices, I made a new emulator.

  • Working device: emulated Nexus 6 running Android 7.1.1 (API 25) with 1 contact

Now I’m at a loss. I even uninstalled the app from the physical Nexus 6 to do a clean install, it still didn’t work. Is there anything I can look at to have it work consistently?

I just realized what the difference was for the non-working device list above. I had turned on the Developer option to “Don’t keep activities”. Hence why the implicit intents were destroying the calling activity. This is now solved and is working as described in the book.

But, the question does remain if the given “broken” scenario can be worked around to still allow data to persist.