From b7bb5198e1936e57cea4af151d330c76f77b6aea Mon Sep 17 00:00:00 2001 From: Tu Nguyen <42457760+tunm1228@users.noreply.github.com> Date: Thu, 11 Sep 2025 18:13:09 +0700 Subject: [PATCH] Update ContactsServicePlugin.java fix crash when go back with android --- .../ContactsServicePlugin.java | 64 +++++++++++-------- 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/android/src/main/java/flutter/plugins/contactsservice/contactsservice/ContactsServicePlugin.java b/android/src/main/java/flutter/plugins/contactsservice/contactsservice/ContactsServicePlugin.java index 8c69de78..5e0291cb 100644 --- a/android/src/main/java/flutter/plugins/contactsservice/contactsservice/ContactsServicePlugin.java +++ b/android/src/main/java/flutter/plugins/contactsservice/contactsservice/ContactsServicePlugin.java @@ -269,41 +269,53 @@ void finishWithResult(Object result) { } } - @Override - public boolean onActivityResult(int requestCode, int resultCode, Intent intent) { - if(requestCode == REQUEST_OPEN_EXISTING_CONTACT || requestCode == REQUEST_OPEN_CONTACT_FORM) { + @Override +public boolean onActivityResult(int requestCode, int resultCode, Intent intent) { + if (requestCode == REQUEST_OPEN_EXISTING_CONTACT || requestCode == REQUEST_OPEN_CONTACT_FORM) { try { - Uri ur = intent.getData(); - finishWithResult(getContactByIdentifier(ur.getLastPathSegment())); + Uri ur = intent.getData(); + finishWithResult(getContactByIdentifier(ur.getLastPathSegment())); } catch (NullPointerException e) { - finishWithResult(FORM_OPERATION_CANCELED); + finishWithResult(FORM_OPERATION_CANCELED); } return true; - } + } - if (requestCode == REQUEST_OPEN_CONTACT_PICKER) { - if (resultCode == RESULT_CANCELED) { - finishWithResult(FORM_OPERATION_CANCELED); - return true; + if (requestCode == REQUEST_OPEN_CONTACT_PICKER) { + try { + if (resultCode == RESULT_CANCELED || intent == null) { + finishWithResult(FORM_OPERATION_CANCELED); + return true; + } + + Uri contactUri = intent.getData(); + if (contactUri == null) { + finishWithResult(FORM_OPERATION_CANCELED); + return true; + } + + Cursor cursor = contentResolver.query(contactUri, null, null, null, null); + if (cursor != null && cursor.moveToFirst()) { + String id = contactUri.getLastPathSegment(); + getContacts("openDeviceContactPicker", id, false, false, false, localizedLabels, this.result); + } else { + Log.e(LOG_TAG, "onActivityResult - cursor.moveToFirst() returns false"); + finishWithResult(FORM_OPERATION_CANCELED); + } + if (cursor != null) { + cursor.close(); + } + } catch (Exception e) { + Log.e(LOG_TAG, "Error in contact picker: " + e.getMessage()); + finishWithResult(FORM_OPERATION_CANCELED); } - Uri contactUri = intent.getData(); - if (intent != null){ - Cursor cursor = contentResolver.query(contactUri, null, null, null, null); - if (cursor.moveToFirst()) { - String id = contactUri.getLastPathSegment(); - getContacts("openDeviceContactPicker", id, false, false, false, localizedLabels, this.result); - } else { - Log.e(LOG_TAG, "onActivityResult - cursor.moveToFirst() returns false"); - finishWithResult(FORM_OPERATION_CANCELED); - }}else{return true;} - cursor.close(); return true; - } - - finishWithResult(FORM_COULD_NOT_BE_OPEN); - return false; } + finishWithResult(FORM_COULD_NOT_BE_OPEN); + return false; +} + void openExistingContact(Contact contact) { String identifier = contact.identifier; try {