5

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?

Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51
AleCat83
  • 1,443
  • 3
  • 22
  • 41

4 Answers4

6

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.

AndrewR
  • 10,759
  • 10
  • 45
  • 56
  • 2
    Overriding onActivityResult from within the containing fragment still does not work as the PlaceSelectionListener still doesn't get fired in my code. So I've found a workaround by overriding the onActivityResult in the container activity and calling the onActivityResult of the fragment from there. Even with this solution the PlaceSelectionListener don't get fired, so I've placed my code to execute after selecting the address in the override onActivityResult of the containing fragment. – AleCat83 Dec 31 '15 at 15:19
  • 1
    Is your ViewPager also contained in a Fragment? You'll need to forward on the onActivityResult(...) call through each level of nested fragment. – Daniel Resnick Jan 04 '16 at 03:20
  • no, the ViewPager is contained inside an Activity. Inside the ViewPager there are some fragments one of which contains the SupportPlaceAutocompleteFragment. – AleCat83 Jan 09 '16 at 14:36
4

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
    }
}
cutiko
  • 9,887
  • 3
  • 45
  • 59
0

Assuming the Google Places API for android services are properly enabled for your api key, even I observed similar issue using SupportPlaceAutocompleteFragment, where the onPlaceSelected is never getting triggered on place selection. However later when i tried with the intent approach, things worked..

Arun VM
  • 71
  • 2
  • 6
0

I ran into this issue this morning, and after a bunch of reading and random similar issues I got the following to work.

If you look at the source code for SupportPlaceAutocompleteFragment you will see it calls

public class SupportPlaceAutocompleteFragment extends Fragment {
    private void zzzG() {
        ...
        try {
            Intent var2 = (new IntentBuilder(2)).setBoundsBias(this.zzaRk).setFilter(this.zzaRl).zzeq(this.zzaRj.getText().toString()).zzig(1).build(this.getActivity());
            this.startActivityForResult(var2, 1);
        }
        ...
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == 1) {
            ...
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}

Depending on your integration this can cause the RequestCode to change to something other than 1. By extending it to override the startActivityForResult you can change the behaviour by calling getActivity().startActivityForResult instead of this.startActivityForResult

public class WrappedSupportPlaceAutocompleteFragment extends SupportPlaceAutocompleteFragment {
@Override
public void startActivityForResult(Intent intent, int requestCode) {
    this.getActivity().startActivityForResult(intent, requestCode);
}

}

from there you can now follow what AndrewR mentioned and pass the activity result from the activity to the fragment and get the expected result.

Izerous
  • 1
  • 1