In my Android application, when I pick a contact from the Contacts activity, instead of returning to the activity that called it, the application closes without any exception or error message.
I tried to launch this activity with different flags, but with no result
class AddPhoneCompatActivity : BaseCompatActivity(), AddPhoneContract.View, View.OnClickListener {
@Inject
lateinit var presenter: AddPhonePresenter
override fun init(savedInstanceState: Bundle?) {
log("Add phone screen - Loading view", LOGS_SIMPLE_FILE_NAME)
setContentView(R.layout.activity_add_phone)
MyLocationNotifierApp.getInjector().inject(this)
presenter.attach(this)
GeneralUtil.checkPermission(
Manifest.permission.READ_CONTACTS,
GeneralUtil.READ_CONTACTS_REQUEST_CODE,
applicationContext,
this
)
presenter.checkIntent(intent)
btnAddPhoneFromContacts.setOnClickListener(this)
btnPhoneNext.setOnClickListener(this)
}
override fun onBackPressed() {
goBack()
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
when(item?.itemId) {
android.R.id.home -> {
goBack()
}
}
return true
}
private fun goBack() {
intent.setClass(this, AddLabelCompatActivity::class.java)
intent.putExtra(GeneralUtil.PHONE_SERIALIZATION_KEY, edAddPhoneNum.text.toString())
startActivity(intent)
finish()
}
override fun onClick(v: View?) {
when (v?.id) {
btnAddPhoneFromContacts.id -> {
log("Starting contacts picker", LOGS_SIMPLE_FILE_NAME)
val intent = Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI)
intent.type = ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE;
startActivityForResult(intent, GeneralUtil.REQUEST_CODE_SEARCH_CONTACT)
}
btnPhoneNext.id -> {
presenter.proceedNext(edAddPhoneNum.text.toString(), intent)
}
}
}
override fun updatePhoneEditField(phoneNum: String?) = edAddPhoneNum.setText(phoneNum)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
log("Add phone screen - onActivityResult called. Checking passed data.", LOGS_SIMPLE_FILE_NAME)
if (requestCode == GeneralUtil.REQUEST_CODE_SEARCH_CONTACT) {
if (resultCode == Activity.RESULT_OK) {
presenter.processAddPhoneRequestFomIntent(data)
}
}
}
}
It is supposed that after picking a contact, I return to the activity and show a dialog with the list of phone numbers corresponding to the contact. But instead, the the app closes without any notification.
Found the reason. The entity creation flow is not using startActivityForResult, but the pieces of data passed between activities. And I started this flow in the starting activity using flags Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NO_HISTORY. When I deleted this line in the starting flow activity, it began working as intended.