Android PlacePicker不返回所选地址

时间:2018-07-03 02:39:25

标签: android google-maps google-places-api

我在Android应用中使用Google PlacePicker。我可以用它来选择地址。但是,一旦选择了地址,它就不会返回到呼叫者活动。相反,我必须按下“后退”按钮!

这是我初始化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;
    }
});

这就是我收听结果的方式:

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();
        }
    }
}

问题是:除非按下位置选择器上的“后退”按钮,否则我不会收到日志消息“ Google PlacePicker finished”!之后,一切正常进行,我要执行的操作正常(这意味着地址已正确选择)

存在类似的问题here,注释表明呼叫者活动清单中可能有android:noHistory="true"。但是我检查了,事实并非如此。这是清单中的相应片段:

<activity
    android:name=".AddressPickerActivity"
    android:theme="@style/AppThemeNoActionBar" />

是什么原因造成的?

EDIT1:

我还尝试了将android:noHistory="false"显式添加到清单中。没有变化。

1 个答案:

答案 0 :(得分:5)

您可以使用OnClickListener或尝试为startActivityForResult添加条件MotionEvent.ACTION_UP,因为碰巧PlacePicker两次打开。

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;
    }
});