silverFoxA
silverFoxA

Reputation: 4659

PlacePicker activity doesn't call `onActivityResult` when nothing is selected

Here is the code that i have tried but when the user doesn't select anything and just return back the activity doesn't call the onActivityResult() method

I called the PlacePIcker as

googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
    @Override
    public void onMapClick(LatLng latLng) {
        try {
            googleMap.clear();
            PlacePicker.IntentBuilder intentBuilder =
                    new PlacePicker.IntentBuilder();
            Intent intent = intentBuilder.build(getApplicationContext());
            startActivityForResult(intent, 5);
        } catch (GooglePlayServicesRepairableException e) {
            e.printStackTrace();
        } catch (GooglePlayServicesNotAvailableException e) {
            e.printStackTrace();
        }
    }
});

The onActivityResult() method

if (requestCode == 5 && resultCode == RESULT_OK) {
    Toast.makeText(this,"here",Toast.LENGTH_SHORT).show();
    if (data != null) {
        Place place = PlacePicker.getPlace(data, this);
        if (place != null) {
            lat = place.getLatLng().latitude;
            lng = place.getLatLng().longitude;
            MarkerOptions marker = new MarkerOptions().position(
                    place.getLatLng()).title("Hello Maps"); marker.icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_RED));
                  googleMap.addMarker(marker);
        } else{
            Toast.makeText(this,"place",Toast.LENGTH_SHORT).show();
            MarkerOptions marker = new MarkerOptions().position(
                    new LatLng(latitude, longitude)).title("Hello Maps");
            lat = latitude;
            lng = longitude;
            // Changing marker icon
            marker.icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

            // adding marker
            googleMap.addMarker(marker);
        }
    } else{
        Toast.makeText(this,"data",Toast.LENGTH_SHORT).show();
        MarkerOptions marker = new MarkerOptions().position(
                new LatLng(latitude, longitude)).title("Hello Maps");
        lat = latitude;
        lng = longitude;
        // Changing marker icon
        marker.icon(BitmapDescriptorFactory
                .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

        // adding marker
        googleMap.addMarker(marker);
    }

When I click the default back button whithin the placepicker activity i don't see any of the toast being called

As per the documentation, it should be returning the place.getplace() as null

When the user selects a place, you can retrieve the place by calling PlacePicker.getPlace(). If the user has not selected a place, the method will return null.

Which means it should go to onActivityResult(), but in my case on back and not selecting any places it's not coming to onActivityResult().

Can anyone please help me out with this??

Upvotes: 2

Views: 1067

Answers (2)

dora
dora

Reputation: 2077

I think you have missed an else statement

    if (requestCode == 5 && resultCode == RESULT_OK) {
        Toast.makeText(this,"here",Toast.LENGTH_SHORT).show();
        if (data != null) {
            Place place = PlacePicker.getPlace(data, this);
            if (place != null) {
                lat = place.getLatLng().latitude;
                lng = place.getLatLng().longitude;
                MarkerOptions marker = new MarkerOptions().position(
                        place.getLatLng()).title("Hello Maps"); marker.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_RED));
                      googleMap.addMarker(marker);
            } else{
                Toast.makeText(this,"place",Toast.LENGTH_SHORT).show();
                MarkerOptions marker = new MarkerOptions().position(
                        new LatLng(latitude, longitude)).title("Hello Maps");
                lat = latitude;
                lng = longitude;
                // Changing marker icon
                marker.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

                // adding marker
                googleMap.addMarker(marker);
            }
        } else{
            Toast.makeText(this,"data",Toast.LENGTH_SHORT).show();
            MarkerOptions marker = new MarkerOptions().position(
                    new LatLng(latitude, longitude)).title("Hello Maps");
            lat = latitude;
            lng = longitude;
            // Changing marker icon
            marker.icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

            // adding marker
            googleMap.addMarker(marker);
        }
}else if(requestCode == 5 && resultCode == RESULT_CANCELED){
    // handle no selection here
}

Upvotes: 1

Shayan Ahmad
Shayan Ahmad

Reputation: 1165

This is a major issue when your application is working over network that is working on the main thread and it wont render appropriate results on UI due to application's business with the main thread. For this please check the android's AsyncTask class that will get you do the job.

you can check youtube link:

Async Task implementation

Android documentation for Async Task

just to get you a quick walk through with the async task, it will implement three functions and on the button where you would want to call this operations to happen on side-thread, you would use AsyncTask.execute(), however:

1) doInBackground() -> your map's function will be called here.

2) onPreExecute() -> tasks or UI updates before the doInBackground is called.

3) onPostExecute() -> after the doInBackground has finished it's job.

Upvotes: 1

Related Questions