英文:
How to prevent Google Places finish my android activity?
问题
我正在尝试在我的应用程序中使用Google Places Android库,它运行得非常好。问题在于,当用户在“AutocompleteSupportFragment”中选择地点/地址或按返回按钮时,会结束当前的活动并返回到主活动。
我的活动是作为“Receiver”的结果打开的活动。
活动调用的代码
```java
Intent requestDriverIntent = new Intent(receiverContext, RequestDeliveryActivity.class);
requestDriverIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 广播接收器所需
requestDriverIntent.putExtra("line", lineItemId);
requestDriverIntent.putExtra("order", orderId);
receiverContext.startActivity(requestDriverIntent);
以下是我如何使用Places响应
private class AddressSelectionListener implements PlaceSelectionListener {
@Override
public void onPlaceSelected(@NonNull Place place) {
deliveryAddress = place.getAddress();
deliveryLat = place.getLatLng().latitude;
deliveryLng = place.getLatLng().longitude;
}
@Override
public void onError(@NonNull Status status) {
errorAlertDialog.show();
}
}
<details>
<summary>英文:</summary>
I'm trying to use Google Places android lib in my app, It works very well. The problem, when user select the place/address in `AutocompleteSupportFragment` or when press the back button, this finish my current activity and returns to the main activity.
My activity is an activity that opens as result of a `Receiver`.
Code of activity invocation
```java
Intent requestDriverIntent = new Intent(receiverContext, RequestDeliveryActivity.class);
requestDriverIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // required by the app who broadcast
requestDriverIntent.putExtra("line", lineItemId);
requestDriverIntent.putExtra("order", orderId);
receiverContext.startActivity(requestDriverIntent);
And here how I use the Places response
private class AddressSelectionListener implements PlaceSelectionListener {
@Override
public void onPlaceSelected(@NonNull Place place) {
deliveryAddress = place.getAddress();
deliveryLat = place.getLatLng().latitude;
deliveryLng = place.getLatLng().longitude;
}
@Override
public void onError(@NonNull Status status) {
errorAlertDialog.show();
}
}
答案1
得分: 0
在你的 RequestDeliveryActivity
中重写 onBackPressed
方法,不要调用 super
,而是留空(Activity
不会在返回键按下时关闭),或者根据需要启动另一个 Activity
,然后调用 finish();
来关闭当前的 Activity
。
@Override
public void onBackPressed() {
// 留空或者启动新的操作
}
英文:
override onBackPressed
method in your RequestDeliveryActivity
, don't call 'super', instead leave empty (Activity
won't close on back press) or start another Activity
if desired and call finish();
to close current Activity
@Override
public void onBackPressed() {
// leave empty or start smth new
}
答案2
得分: 0
使用重写 onBackPressed()
方法在活动中。
尝试这样做:
@Override
public void onBackPressed() {
finish();
}
英文:
use override onBackPressed() method in activity
try this :
@Override
public void onBackPressed() {
finish();
}
答案3
得分: 0
好的,我找到了错误。我只是从清单文件中移除了 android:noHistory="true"
。
英文:
Ok, I found the error. I just remove android:noHistory="true"
from my manifest.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论