I have created a Place[]
type array p
to store the place obtained through fetchPlace()
method. I have just shown the some lines code of a function which I have to call. But when I call this function the 1st logd
line is executed first which shows value of p[0]
as null
. As fetching a place takes some time, so I delayed 2nd logd
line. Here, p[0] is not null. Please have a look at the code below:
final Place[] p = new Place[1];
final String placeId = place.get("place_id");
final List<Place.Field> placeFields = Arrays.asList(
Place.Field.PHONE_NUMBER,
Place.Field.LAT_LNG
);
final FetchPlaceRequest request = FetchPlaceRequest.newInstance(placeId, placeFields);
placesClient.fetchPlace(request).addOnSuccessListener((response) -> {
p[0] = response.getPlace();
}).addOnFailureListener((exception) -> {
Log.i(TAG, "Place not found: " + exception.getMessage());
});
Log.d(TAG, "Place: " + p[0]);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Log.d(TAG, "Place Delayed: " + p[0]);
}
}, 1000);
Here is the two logd
outputs:
1st : D/MapsFragment: Place: null
2nd : D/MapsFragment: Place Delayed: Place{address=null, addressComponents=null, businessStatus=null, attributions=[], id=null, latLng=lat/lng: (19.0255944,73.0935353), name=null, openingHours=null, phoneNumber=+91 1800 208 1234, photoMetadatas=null, plusCode=null, priceLevel=null, rating=null, types=null, userRatingsTotal=null, utcOffsetMinutes=null, viewport=null, websiteUri=null}
I want to ask why does the code inside addOnSuccessListener
doesn't run before logd
which is below it ?
Thanks in advance !