0

I'm trying to read and display contact on TextView. The problem is, I can launch the Contacts activity and select the contact. After that the Parent activity (which launched contacts Activity) is gone. Any idea as to why its not working?

class MainActivity : AppCompatActivity() {
    val REQUEST_CONTACT = 1000

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        takeContact.setOnClickListener {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
                launchContactPicker()

            } else {
                if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CONTACTS)) {
                } else {
                    ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_CONTACTS), REQUEST_CONTACT)
                }

            }
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        when (requestCode) {
            REQUEST_CONTACT -> {
                if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                    launchContactPicker()
                } else {
                    Toast.makeText(this, "Permission denied. You can't read contacts!", Toast.LENGTH_LONG).show()
                }
                return
            }
        }
    }

    private fun launchContactPicker() {
        Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI).also { intent ->
            intent.resolveActivity(packageManager)?.also {
                startActivityForResult(intent, REQUEST_CONTACT)
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
        if (requestCode == REQUEST_CONTACT && resultCode == RESULT_OK) {
            val uri = data.data as Uri
            val projection = arrayOf(ContactsContract.CommonDataKinds.Phone.NUMBER)
            val cursor = contentResolver.query(uri, projection, null, null, null)
            if (cursor != null && cursor.moveToFirst()) {
                val phoneIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
                val nameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)

                val number = cursor.getString(phoneIdx)
                val name = cursor.getString(nameIdx)
                val contact = "$name\n$number"

                selectedContact.text = contact

            }

            cursor.close()
        }
    }
}

I have checked with debugger too, the onActivityResult is not reached. I have tested on Android 6.

Stefano Mtangoo
  • 6,017
  • 6
  • 47
  • 93
  • I don't think you're using the right Intent for this: https://stackoverflow.com/questions/41327416/pick-contact-directly-from-contact-picker-intent – TheWanderer Oct 07 '18 at 21:28
  • I'm not sure I understand what you mean. What is the right intent? The link you provided have many same details as my post. Can you point the specific thing am doing wrong (and correct it may be?) – Stefano Mtangoo Oct 08 '18 at 09:36
  • Look at the answer to that post. It uses a completely different intent for picketing a contact. – TheWanderer Oct 08 '18 at 11:52
  • It doesn't work too. I don't think it is intent issue because it loads the contacts, I can select it and all of the sudded my launching activity is gone – Stefano Mtangoo Oct 08 '18 at 11:59

0 Answers0