I used Google Places AutocompleteFragment for loading address from user. The selected address from the dropdown list working fine inside Activity. But when i use fragment instead of Activity, its not working.
- You need to be more clear with your query! What was working with activity but not with fragment? You mean with the activity you were able to receive the onPlaceSelected callback? Also add more helpful tags to your question like "google-places-api","mapfragment" etc to attract more attention.Uncaught Exception– Uncaught Exception2016-01-05 07:20:02 +00:00Commented Jan 5, 2016 at 7:20
- I am able to get address from activity, but when i use fragment autocomplete callback not workingBala– Bala2016-01-05 10:16:03 +00:00Commented Jan 5, 2016 at 10:16
- Pls share the code depicting how you are using PlaceAutocompleteFragment inside your fragment (including the xml)Uncaught Exception– Uncaught Exception2016-01-06 08:47:17 +00:00Commented Jan 6, 2016 at 8:47
Add a comment |
1 Answer
According to Google Places API for Android, if your fragment is nested within another fragment, your app must also forward onActivityResult() calls from the containing fragment to work around a known limitation of android.support.v4.app.Fragment. This is shown in the following snippet:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); autocompleteFragment.onActivityResult(requestCode, resultCode, data); } 2 Comments
Brendan Cordingley
I've tried this approach but it appears that onActivityResult is not being called. Logcat shows "E/Places: Unknown PlaceAutocompleteAdapter state change."
Brendan Cordingley
Callbacks were not working because my PlaceSelectedListener was not initialized due to incorrect retrieval of my fragment (fragment was null). mAutocompleteFragment = (PlaceAutocompleteFragment) getActivity().getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment); was the correct way to retrieve the fragment in my case.