Skip to content Skip to sidebar Skip to footer

How Can I Put The Updated Phone Number Into The Field I Picked For My Android App?

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. After picking a contact the phone number is pl

Solution 1:

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.


Post a Comment for "How Can I Put The Updated Phone Number Into The Field I Picked For My Android App?"