启动片段正在重新启动父片段

时间:2019-08-21 17:47:27

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

我在同一屏幕上显示带有自动完成搜索栏的Google地图。我的xml布局看起来像这样

<LinearLayout ..>
    <Fragment .../> // the map fragment
    <Fragment .../>  // the auto complete support fragment
</LinearLayout>

当您单击AutoCompleteSupportFragment搜索栏时,它将打开覆盖窗口,以便您可以进行搜索。一旦撤消它,我从日志中注意到MAP片段正在重新启动(再次调用OnCreate和OnCreateView)。

好像显示嵌套片段正在重置父片段。这是一个问题,因为无论我丢失了什么信息,我都无法将选择的地点从酒吧发送回地图上

有帮助吗?

PS:没有代码可添加嵌套片段。这些片段是Google片段,并以XML定义。

1 个答案:

答案 0 :(得分:0)

您可能要考虑使用PlaceSelectionListener,它可能看起来像这样:

// Initialize the AutocompleteSupportFragment.
AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
        getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);

// Specify the types of place data to return.
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));

// Set up a PlaceSelectionListener to handle the response.
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
    @Override
    public void onPlaceSelected(Place place) {
        // TODO: Get info about the selected place.
        Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
    }

    @Override
    public void onError(Status status) {
        // TODO: Handle the error.
        Log.i(TAG, "An error occurred: " + status);
    }
});

您可以在documents中找到此示例以及如何正确实现此示例的其他示例。

相关问题