Method only works when minimizing/maximizing app

Asked

Viewed 70 times

1

I’m developing an app for TCC using Google Places API. A part of the code returns the locations/establishments next to the user. However, when I run the app and enter the responsible class I have no return. But, if I minimize the app and then maximize and then re-request the return method, then the sites are returned.

That is, I believe to be a problem related to the life cycle of Activity, or use of AssyncTask, but I don’t have enough knowledge about it and my research didn’t help me solve the problem.

The code below refers to the class in question:

 public class PlacesAPIActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener{

    private static final String LOG_TAG = "PlacesAPIActivity";
    private static final int GOOGLE_API_CLIENT_ID = 0;
    private GoogleApiClient mGoogleApiClient;
    private static final int PERMISSION_REQUEST_CODE = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_places_api);

        Button currentButton = (Button) findViewById(R.id.current_button);
        mGoogleApiClient = new GoogleApiClient.Builder(PlacesAPIActivity.this).addApi(Places.PLACE_DETECTION_API).enableAutoManage(this, GOOGLE_API_CLIENT_ID, this).build();

        currentButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mGoogleApiClient.isConnected()) {
                    if (ContextCompat.checkSelfPermission(PlacesAPIActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION)
                            != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(PlacesAPIActivity.this,
                                new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                                PERMISSION_REQUEST_CODE);
                    } else {
                        callPlaceDetectionApi();
                    }
                }
            }
        });

    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        Log.e(LOG_TAG, "Google Places API connection failed with error code: "
                + connectionResult.getErrorCode());

        Toast.makeText(this,
                "Google Places API connection failed with error code:" +
                        connectionResult.getErrorCode(),
                Toast.LENGTH_LONG).show();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE:
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    callPlaceDetectionApi();
                }
                break;
        }
    }

    private void callPlaceDetectionApi() throws SecurityException {
        PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi
                .getCurrentPlace(mGoogleApiClient, null);
        result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
            @Override
            public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
                for (PlaceLikelihood placeLikelihood : likelyPlaces) {
                    Log.i(LOG_TAG, String.format("Place '%s' with " +
                                    "likelihood: %g",
                            placeLikelihood.getPlace().getName(),
                            placeLikelihood.getLikelihood()));
                }
                likelyPlaces.release();
            }
        });
    }
 }
  • Not missing code? onCreate only runs the first time you open the app.

  • So even the first time onCreate runs the return of the callPlaceDetectionApi() method is not given in the Log. In the Places documentation it looks exactly like this: https://developers.google.com/places/android-api/current-place?hl=pt-br What is the best way to ensure that the callPlaceDetectionApi() method will be executed? Because my goal is that in the App the main screen load a list with the nearest places/ establishments, IE, this list has to be ready the moment the View goes to screen.

  • You can display the view only when returning callplacedetectionApi

  • And how do I do that? rs

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.