title: registerForActivityResult and onActivityResult onActivityResult in Kotlin
body: <p>I am currently trying to learn from a step-by-step tutorial to upload an Image or File to my server while using Volley. This tutorial is a little bit outdated and I really don't understand how I can fix these issues.</p>
  <p><a href="https://betterprogramming.pub/how-to-upload-an-image-file-to-your-server-using-volley-in-kotlin-a-step-by-step-tutorial-23f3c0603ec2" rel="nofollow noreferrer">the tutorial</a></p>
  <ol>
  <li><p><code>onActivityResult(Int, Int, Intent?): Unit' is deprecated. Deprecated in Java</code></p>
  </li>
  <li><p><code>Fragment is attempting to registerForActivityResult after being created. Fragments must call registerForActivityResult() before they are created (i.e. initialization, onAttach(), or onCreate()).</code></p>
  </li>
  </ol>
  <p>My code</p>
  <pre class="lang-kotlin prettyprint-override"><code>//Uploading Photos
      private fun launchGallery() {
          val intent = Intent(Intent.ACTION_PICK)
          intent.type = &quot;image/*&quot;
          var resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -&gt;
              if (result.resultCode == Activity.RESULT_OK) {
                  // There are no request codes
                  val data: Intent? = result.data
              }
          }
      }
  
      private fun uploadImage() {
          imageData?: return
          val request = object : VolleyFileUploadRequest(
              Request.Method.POST,
              postURL,
              {
                  println(&quot;response is: $it&quot;)
              },
              {
                  println(&quot;error is: $it&quot;)
              }
          ) {
              override fun getByteData(): MutableMap&lt;String, FileDataPart&gt; {
                  var params = HashMap&lt;String, FileDataPart&gt;()
                  params[&quot;imageFile&quot;] = FileDataPart(&quot;image&quot;, imageData!!, &quot;jpeg&quot;)
                  return params
              }
          }
          Volley.newRequestQueue(requireContext()).add(request)
      }
  
      @Throws(IOException::class)
      private fun createImageData(uri: Uri) {
          val inputStream = requireContext().contentResolver.openInputStream(uri)
          inputStream?.buffered()?.use {
              imageData = it.readBytes()
          }
      }
  
      override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
          if (resultCode == Activity.RESULT_OK &amp;&amp; requestCode == IMAGE_PICK_CODE) {
              val uri = data?.data
              if (uri != null) {
                  imageView.setImageURI(uri)
                  createImageData(uri)
              }
          }
          super.onActivityResult(requestCode, resultCode, data)
      }
  }
  
  </code></pre>