Search code examples
androidandroid-activitymethods

Waiting the methods' outputs to start an Activity


I have a method, called getLocation() that find the current device's location thanks to FusedLocationClient. After the invocation of this method I get the latitude and longitude of the location found, and with putExtra I pass these values to another Activity. In getLocation() i have a Toast that print on the display latitude and longitude, and I receive the correct position. In the activity I have another Toast that display the values of latitude and longitude that i had sent from the MainActivity but the result is that latitude and longitude are both null. If I go back and restart the activity i get not null values of latitude and longitude. Any idea about this?

OTHER CODE:

case R.id.nearYou:
            i = new Intent(MainActivity.this, Ricerca1.class);
            if (!isGPS) {
                Toast.makeText(this, "Please turn on GPS", 
Toast.LENGTH_SHORT).show();
                return;
            }
            isContinue = false;
            getLocation();

            latitudine = wayLatitude.toString();
            longitudine = wayLongitude.toString();


            i.putExtra("latPar", longitudine);
            i.putExtra("lonPar", latitudine);
            i.putExtra("radiusPar", "0.5");
            i.putExtra("categoryPar", "Museum;Botanical_and_zoological_gardens;Churches;Cultural_centre;Cultural_sites;Historical_buildings;Library;Monument_location;Photographic_activities;Squares;Theatre;");
            startActivity(i);

            break; 

GETLOCATION:

private void getLocation() {
    if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
                AppConstants.LOCATION_REQUEST);

    } else {
        if (isContinue) {
            mFusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null);
        } else {
            mFusedLocationClient.getLastLocation().addOnSuccessListener(MainActivity.this, location -> {
                if (location != null) {
                    wayLatitude = location.getLatitude();
                    wayLongitude = location.getLongitude();
                    Toast.makeText(MainActivity.this,String.format(Locale.US, "%s - %s", wayLatitude, wayLongitude), Toast.LENGTH_SHORT).show();

                } else {
                    mFusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null);
                }
            });
        }
    }

}

Solution

  • You should startActivity after Toast message in getLocation().

    Why?

    Because, getLocation waiting for completion of the listener (addOnSuccessListener) and you start the activity before the completion.

    Your code will be:

        private void getLocation() {
            if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
                        AppConstants.LOCATION_REQUEST);
    
            } else {
                if (isContinue) {
                    mFusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null);
                } else {
                    mFusedLocationClient.getLastLocation().addOnSuccessListener(MainActivity.this, location -> {
                        if (location != null) {
                            wayLatitude = location.getLatitude();
                            wayLongitude = location.getLongitude();
                            Toast.makeText(MainActivity.this,String.format(Locale.US, "%s - %s", wayLatitude, wayLongitude), Toast.LENGTH_SHORT).show();
    
                            //Code from OTHER CODE
                            latitudine = wayLatitude.toString();
                            longitudine = wayLongitude.toString();
    
                            i.putExtra("latPar", longitudine);
                            i.putExtra("lonPar", latitudine);
                            i.putExtra("radiusPar", "0.5");
    
                            i.putExtra("categoryPar", "Museum;Botanical_and_zoological_gardens;Churches;Cultural_centre;Cultural_sites;Historical_buildings;Library;Monument_location;Photographic_activities;Squares;Theatre;");
    
                            startActivity(i);
    
                        } else {
                            mFusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null);
                        }
                    });
                }
            }
    
        }