android - Android PlacePicker 不返回所选地址
问题描述
我在我的 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
问题是:除非我按下地点选择器上的后退按钮,否则我不会收到日志消息“ ”!之后一切正常,并且我想要发生的操作正常工作(意味着地址已正确选择)
这里有一个类似的问题,评论表明调用者活动可能android:noHistory="true"
在清单中。但我查了一下,不是。这是我清单中的相应片段:
<activity
android:name=".AddressPickerActivity"
android:theme="@style/AppThemeNoActionBar" />
这可能是什么原因造成的?
编辑1:
我还尝试明确添加android:noHistory="false"
到清单中。没变。
解决方案
您可以使用OnClickListener
或尝试MotionEvent.ACTION_UP
为 startActivityForResult 添加 if 条件,因为 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;
}
});
推荐阅读
- sml - 在 SML 中计算和打印匹配值
- json - React 渲染 JSON 嵌套对象
- flutter - 颤振处理后退按钮后的状态
- python - 如何创建一个空数据框并循环插入数据?
- reactjs - 使用 dataSource 时 GraphQL 查询返回 null
- ios - UITableview 按钮在滚动时突出显示
- android-studio - 使用 Android-Studio 时未安装 Flutter 插件
- mysql - 烧瓶 check_password 总是返回 false
- django - 使用 django 频道和 websocket 添加多个组
- xml - XSLT - 仅在子项包含特定文本时显示