Reputation: 1347
I'm using Google PlacePicker in my Android app. I'm able to use it to select addresses. However, it does not return to the caller activity once an address is selected. Instead, I have to press the back button!
This is how I initialize my PlacePicker:
/**
* Add New Address
*/
mNewButton.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try
{
startActivityForResult(builder.build(AddressPickerActivity.this), ADDRESS_CODE);
}
catch (GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException e)
{
int status = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(AddressPickerActivity.this);
GoogleApiAvailability.getInstance().getErrorDialog(AddressPickerActivity.this, status, 100).show();
}
return false;
}
});
and this is how I listen for the result:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == ADDRESS_CODE)
{
if (resultCode == RESULT_OK)
{
Log.d(TAG, "Google PlacePicker finished");
// Do Stuff then finish current activity too
AddressPickerActivity.this.setResult(RESULT_OK);
AddressPickerActivity.this.finish();
}
}
}
The problem is: I do not get the log message "Google PlacePicker finished
" unless I press the back button on the place picker! After that everything proceeds as normal, and the actions I want to happen work fine (meaning the address has been picked correctly)
There's a similar question here and the comments suggest that the caller activity might have android:noHistory="true"
in the manifest. But I checked and it's not. Here's the corresponding snippet from my manifest:
<activity
android:name=".AddressPickerActivity"
android:theme="@style/AppThemeNoActionBar" />
What can be causing this?
I also tried explicitly adding android:noHistory="false"
to the manifest. No change.
Upvotes: 2
Views: 264
Reputation: 1332
You can use OnClickListener
or try to add if condition MotionEvent.ACTION_UP
for startActivityForResult because it happens that PlacePicker is opens twice.
mNewButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(motionEvent.getAction() == MotionEvent.ACTION_UP){
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
startActivityForResult(builder.build(AddressPickerActivity.this), ADDRESS_CODE);
} catch (GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException e) {
int status = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(AddressPickerActivity.this);
GoogleApiAvailability.getInstance().getErrorDialog(AddressPickerActivity.this, status, 100).show();
}
}
return false;
}
});
Upvotes: 1