1

Although everything work as intended and I get results from the Places API, the onActivityResult callback is never called when the user selects a result. What happens is that the result appears in the search textview and the Autocomplete activity doesn't exit. I have to press back for it to close.

 // Set the fields to specify which types of place data to return.
            val fields = Arrays.asList(Place.Field.ID, Place.Field.NAME)
            // Start the autocomplete intent.
            val intent: Intent = Autocomplete.IntentBuilder(
                AutocompleteActivityMode.OVERLAY, fields
            ).setTypeFilter(TypeFilter.ADDRESS).
                    setCountry("DE")
                .build(requireContext())
            startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE)

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if(data!=null) {
        var status: Status = getStatusFromIntent(data)
        //var place: Place = getPlaceFromIntent(data)
    }
    super.onActivityResult(requestCode, resultCode, data)
}
Katerina A.
  • 1,268
  • 10
  • 24

3 Answers3

1

Do you run method startActivityForResult inside Fragment? Check this out: onActivityResult is not being called in Fragment

Jacek
  • 63
  • 8
0

You have to override onActivityResult in Activity which contains your fragment.

// In your activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
}

This will call your fragment's onActivityResult()

Jakir Hossain
  • 3,830
  • 1
  • 15
  • 29
0

Ensure you're using the latest version of Place client SDK Places 2.1.0 . I don't see any reason in your code that causes the behavior you've addressed. Just use the below implementation and try.

private fun pickNewPlace() {
        if (!Places.isInitialized()) {
            Places.initialize(context!!, getString(R.string.google_service_api_key))
        }

        // Set the fields to specify which types of place data to return.
        val fields = listOf(Place.Field.ID, Place.Field.NAME)

        // Start the autocomplete intent.
        val intent = Autocomplete.IntentBuilder(
            AutocompleteActivityMode.OVERLAY, fields
        ).setTypeFilter(TypeFilter.ADDRESS).setCountry("DE").build(context!!)

        startActivityForResult(intent, REQUEST_CODE_DEFAULT)
    }

onActivityResult result handling

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == REQUEST_CODE_DEFAULT) {
            if (resultCode == RESULT_OK) {
                // Get relevent info from place object 
                val place = Autocomplete.getPlaceFromIntent(data!!)

            } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
                val status = Autocomplete.getStatusFromIntent(data!!)
                showAlertMessage(status.statusMessage ?: getString(R.string.something_went_wrong))
            }
        }
    }
Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73