'Get current location to find the nearest places

I want to when I log in to the app I will get current location user then I will pass it to main activity.

private LocationRequest locationRequest;
private FirebaseAuth mAuth = FirebaseAuth.getInstance();
String state = "";

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

    locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(1000);
    locationRequest.setFastestInterval(500);

    FirebaseUser currentUser = mAuth.getCurrentUser();

    if (ActivityCompat.checkSelfPermission(LoginActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        //granted
        if (isGPSEnabled()) {
            getCurrentLocation(new IListenerLocation() {
                @Override
                public void onCallBack(String state1) {
                    Log.d("state1", state1);
                    state = state1;
                }
            });
        } else {
            turnOnGPS();
        }
    } else {
        //denied
        ActivityCompat.requestPermissions(LoginActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 44);
    }
    
    if (currentUser != null) {
        if (state != "") {
            Log.d("state1", state);

            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            intent.putExtra("state", state);
            startActivity(intent);
            finish();
        }
    }

    addControls();

    addEvents();
}

I want to get location before startActivity. How can I do that? If I copy code get current location to if(currentUser != null) then when I login new user. I can't get current location



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source