OnPlaceSelectedListener of SupportPlaceAutocomplet

2019-02-28 15:36发布

I'm having a problem with the OnPlaceSelectedListener method of a SupportPlaceAutocompleteFragment.

My onViewCreated() method:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    // Initialise a new fragment inside of this one.
    mFragmentManager = getChildFragmentManager();
    mSupportMapFragment = (SupportMapFragment) mFragmentManager.findFragmentByTag("mapFragment");
    mSupportPlaceAutocompleteFragment = (SupportPlaceAutocompleteFragment) mFragmentManager.findFragmentByTag("autocompleteFragment");

    // Never inflate fragments inside other fragments in a layout.xml!
    // Do it programmatically.
    // See here for reference: https://stackoverflow.com/a/19815266/4938112.
    if (mSupportMapFragment == null) {
        mSupportMapFragment = new SupportMapFragment();
        fragmentTransaction(mFragmentManager, mSupportMapFragment, R.id.map_fragment_container, "mapFragment");
    }

    // Asynchronous thread to load map.
    mSupportMapFragment.getMapAsync(this);


    if (mSupportPlaceAutocompleteFragment == null) {
        mSupportPlaceAutocompleteFragment = new SupportPlaceAutocompleteFragment();
        fragmentTransaction(mFragmentManager, mSupportPlaceAutocompleteFragment, R.id.card_view, "autocompleteFragment");
    }

    // Filter for a specific place type.
    AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
            .setTypeFilter(AutocompleteFilter.TYPE_FILTER_CITIES)
            .build();
    mSupportPlaceAutocompleteFragment.setFilter(typeFilter);

    Log.d("I'M HERE", "Hello.");

    mSupportPlaceAutocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            // TODO: Get info about the selected place.
            Log.i("PLACE", "Place: " + place.getName());
            int flag = 1;
            Log.d("FLAG", "flag: " + flag);
        }

        @Override
        public void onError(Status status) {
            // TODO: Handle the error.
            Log.i("PLACE_ERROR", "An error occurred: " + status);
            int flag = 0;
            Log.d("FLAG", "flag: " + flag);
        }
    });

    Log.d("I'M HERE, TOO", "Hello.");

}

When I select a place (all the APIs are enabled and I've a Google Key), the AutoCompleteFragment just closed and nothing happens on the map. The mSupportPlaceAutocompleteFragment.setOnPlaceSelectedListener(...) method is not being fired.

Any hints?

My problem is similar to the one in this question.

I'm using nested fragments, too and I can't understand how to see the onActivityResult() method in this case.

1条回答
在下西门庆
2楼-- · 2019-02-28 16:30

Finally solved this problem: hope it will help someone.

I added this infamous line in the onActivityResult() method in my MainActivity

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    ...
}

and then did put the same method in my MapFragment

    @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
}

Now the nested fragment is sending back its data.

查看更多
登录 后发表回答