Two problems with revokeUriPermission()

I have two separate problems with the call

requireActivity().revokeUriPermission(photoUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION)

The first is when the call is only in onActivityResult(). This is throwing the same error as described in Problem with LiveData upon return from camera except this time photoUri is not initialized. It is the exact same reasoning as the other thread - the database call has not been resolved yet.

The second is when the call in onDetach(). This time the error is

java.lang.SecurityException: Permission Denial: writing 
  androidx.core.content.FileProvider uri 
  IMG_461f153a-34b8-43fe-aaf4-9696fae09dd7.jpg from pid=23835, 
  uid=10059 requires the provider be exported, or grantUriPermission()

This is too be expected from the onDetach() method. The life cycle flow when the camera button is pressed is:

  1. photoButton.onClickListener()
  2. requireActivity().grantUriPermission()
  3. startActivityForResult()
  4. CrimeFragment::onPause()
  5. MainActivity::onDestroy()
  6. CrimeFragment::onDestroyView()
  7. CrimeFragment::onDestroy()
  8. CrimeFragment::onDetach()
  9. requireActivity().revokeUriPermission()
  10. CrimeListFragment::onDestroy()
  11. CrimeListFragment::onDetach()

so when the the camera activity is completed, it already has had the Write permission revoked.

I don’t know how to solve either of these issues because the fragment is being completely destroyed so any life cycle method will fire.

As I just noted in Suspect name is not saving to database, due to the Developer Option of “Don’t keep activities” being set this task wasn’t working. Unchecking that option, the camera now works as anticipated.