Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnPlaceSelectedListener of SupportPlaceAutocompleteFragment not fired inside ViewPager

My application has and activity with a viewPager and 4 fragments. In one fragment I added a google SupportPlaceAutocompleteFragment and

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.area_swipe_fragment, container, false);



    SupportPlaceAutocompleteFragment autocompleteFragment = (SupportPlaceAutocompleteFragment)
            getChildFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            Toast.makeText(ctx, "do something", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(Status status) {
        }
    });






    return view;
}

My problem is the onPlaceSelectedListener is never fired and I can't get the result of the selected place. Instead get fired the onActivityResults() placed in the second fragment. I tried with a onActivityResults() in the first fragment but it doesn't get fired, always executed the one in the second fragment.

Any ideas?

like image 844
AleCat83 Avatar asked Dec 29 '15 08:12

AleCat83


2 Answers

From the docs:

If your fragment is nested within another fragment, your app must also forward onActivityResult() calls to the containing fragment, as shown in the following snippet:

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

Perhaps this is the solution.

like image 68
AndrewR Avatar answered Nov 03 '22 20:11

AndrewR


The @AndrewR answer took me on the right track, very similar to the comments, I override onActivityResult however it didn't work.

The problem was the host Activity wasn't calling the super:

public class HostActivity extends AppCompatActivity {

    //Please don't forget other relevant methods

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //super.onActivityResult... is the important line
        //Add other custom behaviours
    }
}
like image 22
cutiko Avatar answered Nov 03 '22 18:11

cutiko