1

How can I put the updated phone number into the field I picked? I have the picker returning the phone number but in the wrong field.

enter image description here

After picking a contact the phone number is placed in the wrong field.

enter image description here

My fragment responsible for this contact picker logic is in my repo https://github.com/jackygrahamez/MayDay/blob/gradle2/app/src/main/java/com/mayday/md/common/ContactPickerFragment.java

How might I refactor this code to put the number in the correct field?

02-23 12:47:59.217  12360-12360/com.mayday.md E/WizardActivity﹕ onUserInteraction
02-23 12:47:59.347  12360-12360/com.mayday.md E/WizardActivity﹕ onUserInteraction
02-23 12:47:59.347  12360-12360/com.mayday.md E/WizardActivity.onPause﹕ page = setup-contacts
02-23 12:47:59.347  12360-12360/com.mayday.md E/>>>>>>﹕ assert flagRiseFromPause = true
02-23 12:47:59.387  12360-12360/com.mayday.md D/AbsListView﹕ unregisterIRListener() is called
02-23 12:48:00.657  12360-12360/com.mayday.md D/WizardActivity.onStop﹕ page = setup-contacts
02-23 12:48:00.657  12360-12360/com.mayday.md D/AbsListView﹕ onVisibilityChanged() is called, visibility : 4
02-23 12:48:00.657  12360-12360/com.mayday.md D/AbsListView﹕ unregisterIRListener() is called
02-23 12:48:02.107  12360-12360/com.mayday.md E/ContactPickerFragment﹕ onActivityResult requestCode 65636
02-23 12:48:02.107  12360-12360/com.mayday.md E/ContactPickerFragment﹕ onActivityResult resultCode -1
02-23 12:48:02.107  12360-12360/com.mayday.md E/ContactPickerFragment﹕ onActivityResult data Intent { dat=content://com.android.contacts/data/2369 flg=0x1 }
02-23 12:48:02.117  12360-12360/com.mayday.md E/ContactPickerFragment﹕ onActivityResult id 2369
02-23 12:48:02.117  12360-12360/com.mayday.md E/ContactPickerFragment﹕ onActivityResult name XXX XXXX
02-23 12:48:02.147  12360-12360/com.mayday.md D/dalvikvm﹕ GC_FOR_ALLOC freed 598K, 22% free 29241K/37368K, paused 15ms, total 15ms
02-23 12:48:02.147  12360-12360/com.mayday.md E/WizardActivity﹕ onActivityResult pCur android.content.ContentResolver$CursorWrapperInner@4313d048
02-23 12:48:02.147  12360-12360/com.mayday.md E/ContactPickerFragment﹕ onActivityResult phone xxxxxxxxxx
02-23 12:48:02.147  12360-12360/com.mayday.md E/ContactPickerFragment﹕ onActivityResult phone xxxxxxxxxx
02-23 12:48:02.147  12360-12360/com.mayday.md E/ContactPickerFragment﹕ onActivityResult phoneNumberEditText android.widget.EditText{42c580b0 VFED..CL ........ 0,0-944,156 #7f0b0016 app:id/contact_edit_text}
02-23 12:48:02.147  12360-12360/com.mayday.md E/??????﹕ text changed
02-23 12:48:02.147  12360-12360/com.mayday.md D/WizardActivity.onStart﹕ page = setup-contacts
02-23 12:48:02.147  12360-12360/com.mayday.md E/WizardActivity.onResume﹕ pageId = setup-contacts and flagRiseFromPause = true
02-23 12:48:02.147  12360-12360/com.mayday.md E/WizardActivity.onResume﹕ back button pressed
02-23 12:48:02.147  12360-12360/com.mayday.md D/AbsListView﹕ onVisibilityChanged() is called, visibility : 0
02-23 12:48:02.147  12360-12360/com.mayday.md D/AbsListView﹕ unregisterIRListener() is called
02-23 12:48:02.167  12360-12360/com.mayday.md D/AbsListView﹕ unregisterIRListener() is called

NEW CLUE : I notice that the request code returns different values depending on the field I pick: 1st field requestCode 65636, 2nd field requestCode 131172, 3rd field requestCode 196708

Jack Shultz
  • 2,031
  • 2
  • 30
  • 53
  • What is the java class used for the provided screenshot? The layout of the fragment link you provided has only 2 elements in it. – Vamsi Feb 23 '15 at 18:57
  • The methods are inside the class provided `ContactPickerFragment`. `phoneNumberEditText` seems to apply to all three of the phone number fields. I can set them all on creation `onCreateView` by providing phoneNumberEditText.setText('xxxxxxxxxx') . You can see the log output does pertain to this class `onActivityResult phoneNumberEditText` line 118. – Jack Shultz Feb 23 '15 at 19:27
  • Also I notice that the request code returns different values depending on the field I pick: 1st field requestCode 65636, 2nd field requestCode 131172, 3rd field requestCode 196708 – Jack Shultz Feb 23 '15 at 19:32
  • `fragment_type_interactive_contacts.xml` has the the three fragments listed for the contacts fields. Maybe I can define a static EditView and reference them depending on the requestCode returned? – Jack Shultz Feb 23 '15 at 19:49
  • i check your github code look like not tally with your screen, because based on the contact_picker_fragment.xml only has one textfield with image icon. did you mind update the latest source? – zeisuke Feb 24 '15 at 08:22
  • My code is up to date. The three fields are listed in https://github.com/jackygrahamez/MayDay/blob/gradle2/app/src/main/res/layout/fragment_type_interactive_contacts.xml – Jack Shultz Feb 25 '15 at 03:02
  • Where do you use `ContactPickerFragment `? – Marcus Feb 26 '15 at 13:53
  • the layout is MayDay/app/src/main/res/layout/fragment_type_interactive_contacts.xml while the class is located MayDay/app/src/main/java/com/mayday/md/common/ContactPickerFragment.java – Jack Shultz Feb 26 '15 at 14:11
  • I did not manage to fix, but I think I know what's wrong here. The problem is that you use the `contact_picker_fragment` three times. Since the fragment contains the `EditText` with id `contact_edit_text` and you inflate this view three times, the other two references to the `EditText` gets lost. Hence, when you try to add the `phone` to the edit text, it's not found. – Marcus Feb 26 '15 at 14:47
  • You will need to be able to separate the three `EditText` from each other, and somehow keep the reference to each `EditText` at runtime – Marcus Feb 26 '15 at 14:49
  • That is sort of what I gathered. Would it help if I got a chat room? – Jack Shultz Feb 26 '15 at 14:57
  • It would, but I have a meeting in half an hour. Do you have skype? – Marcus Feb 26 '15 at 14:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/71791/discussion-between-marcus-and-jack-shultz). – Marcus Feb 26 '15 at 15:02
  • @JackShultz Hi, I browsed through your project and have identified the problem. If you're still looking for a solution, I can help. – Vikram Feb 26 '15 at 22:29
  • @Vikram I do need a solution still Marcus and I had a look but we both had to run off to meetings today and could not devote sufficient time. If you got the solution then post it. If it works for me I'll give you the bounty! – Jack Shultz Feb 27 '15 at 03:47
  • @JackShultz Could we chat? – Vikram Feb 27 '15 at 04:36
  • sure I'm in http://chat.stackoverflow.com/rooms/71791/discussion-between-marcus-and-jack-shultz – Jack Shultz Feb 27 '15 at 04:38
  • Did you manage to fix it already? From the looks of it you are running into a nested fragment issue: Android OS is not smart enough to route an onActivityResult() call back to the correct nested fragment. Something similar to this: https://stackoverflow.com/questions/28496682/onactivityresult-not-call-in-the-fragment/28577980#28577980. But it is a bit hard to analyse as you already changed your code in your repo. :) – Jeroen Mols Mar 02 '15 at 07:10
  • 2
    Yes i got a fix from the two commenters here. I was waiting for either to claim bounty. – Jack Shultz Mar 02 '15 at 13:50

1 Answers1

2

Your current setup has WizardActivity as the parent activity, SetupContactsFragment as a fragment, and ContactPickerFragment as a child-fragment. When ContactPickerFragment issues a startActivityForResult(...) call, onActivityResult(...) callback is received in WizardActivity.

Problem:

First off, WizardActivity's member variable contactPickerFragment is never used. It isn't part of your ui. So, calling contactPickerFragment.onActivityResult(....) inside WizardActivity#onActivityResult(...) does nothing other than print a few log statements. Additionally, the call to super.onActivityResult(...) is missing altogether. The correct way would be to check if the request code was issued by WizardActivity. If it wasn't, calling the super method will route the onActivityResult(..) call to the fragment SetupContactsFragment.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // currently, WizardActivity does not deal with 
    // any onActivityResult callbacks
    super.onActivityResult(requestCode, resultCode, data);
}

SetupContactsFragment can now receive the onActivityResult(...) callback. Still, we need to identify and dispatch onActivityResult(...) to the correct child-fragment. One way of doing this is to assign a different requestCode to each of the child-fragments. Inside SetupContactsFragment#onActivityResult(...), we iterate over all child-fragments and issue a call to their onActivityResult(...) method. Since we have assigned a different requestCode to each fragment, only one of these calls will be processed.

However, I don't see why you need three identical child fragments, each holding an input field & a button. These widgets can all be part of SetupContactsFragemets' ui. Even if the specifications change from 3 contacts to 10 in the future, you could implement a method that inflates and adds each row multiple times.

In this case, you will need 3 unique requestCodes. Based on which ImageButton is pressed, a different requestCode is used for startActivityForResult(...). Inside onActivityResult(...), the requestCode will indicate which EditText needs to be updated.

Vikram
  • 51,313
  • 11
  • 93
  • 122
  • Can you please help me in following issue. I have been trying hard on this and I see yours this post is related to nested fragments. I will be highly obliged. Here is the post :- http://stackoverflow.com/questions/32240138/oncreateview-of-nested-fragment-is-not-called – Nicks Sep 04 '15 at 02:56