Listing 16.12 - Solution for Depreciated startActivityForResult

Fragment::startActivityForResult was depreciated in Fragment 1.3.0.

Jeff Paone gave a solution to get contact information in the last chapter. I beat my head against the wall for a long time trying to do a similar approach for the camera to no avail.

I learned a lot of neat new stuff along the way about using the camera directly to save to a file using CameraX, but I finally found this great article to show the right way to kick off the camera app to take a photo.

And best of all it only needed a couple lines of code change to Listing 16.12:

        photoButton.apply {
            setOnClickListener {
                Log.d (TAG,"Photo File $photoUri")
                //startActivityForResult(captureImage, REQUEST_PHOTO)

    val takePicture = registerForActivityResult(ActivityResultContracts.TakePicture()) { success: Boolean ->
        if (success) {
            // The image was saved into the given Uri -> do something with it
            Log.d (TAG, "We took a picture...")
            updatePhotoView()    // You'll need this later for listing 16.16