title: Getting a Contact Without Permissions Using registerForActivityResult
body: <p>New to Kotlin and Android development and, after updating some libraries, I had some issue with my previously working method of grabbing Contact details, so I decided to try out the new registerForActivityResult defined at <a href="https://developer.android.com/training/basics/intents/result" rel="nofollow noreferrer">https://developer.android.com/training/basics/intents/result</a> instead of debugging my “legacy” code.  In doing so, I’m no longer able to get this to work without requiring READ_CONTACTS permissions, so I’m hoping someone can hit me with a clue stick and help me bend the new methods to do what the previous method did.</p>
  <p>Thank you.</p>
  <p>Previous method</p>
  <pre><code>override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
      super.onViewCreated(view, savedInstanceState)
      fab.setOnClickListener {
          val intent = Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI).also {
              it.type = ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE
              startActivityForResult(it, 1)
          }
      }
  }
  
  override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
      if (requestCode == 1 &amp;&amp; resultCode == RESULT_OK) {
          val projection = arrayOf(
                  ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY,
                  ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                  ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER
          )
  
          GlobalScope.launch {
              data?.data?.also { contactUri -&gt;
                  context?.contentResolver?.query(contactUri, projection, null, null, null)?.apply {
                      moveToFirst()
                      //val id = viewModel.addPerson(DefaultPerson(getString(0), getString(1), getString(2)))
                      close()
                  }
              }
          }
      }
  }
  </code></pre>
  <p>New method</p>
  <pre><code>private val getPerson = registerForActivityResult(PickContact()) {
      val projection = arrayOf(
              ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY,
              ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
              ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER
      )
  
      context?.contentResolver?.query(it!!.toUri(), projection, null, null, null)?.apply {
          moveToFirst()
          //val id = viewModel.addPerson(DefaultPerson(getString(0), getString(1), getString(2)))
          close()
      }
  }
  
  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
      super.onViewCreated(view, savedInstanceState)
      fab.setOnClickListener {
          getPerson.launch(0)
      }
  }
  
  class PickContact : ActivityResultContract&lt;Int, String?&gt;() {
      override fun createIntent(context: Context, input: Int?) =
              Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI).also {
                  it.type = ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE
              }
  
      override fun parseResult(resultCode: Int, intent: Intent?): String? {
         return if (resultCode == RESULT_OK) intent?.toUri(0) else null
      }
  }
  </code></pre>