如何防止Google Places结束我的Android活动?

huangapple go评论85阅读模式
英文:

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&#39;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(&quot;line&quot;, lineItemId);
requestDriverIntent.putExtra(&quot;order&quot;, 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=&quot;true&quot; from my manifest.

huangapple
  • 本文由 发表于 2020年8月20日 14:16:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63499321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定