I have this code
public String getPoolValue() {
final DocumentReference docRef = database.collection("pool").document("bq2a7gLnz9bpEyIyQeNz");
docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
Pool pool = documentSnapshot.toObject(Pool.class);
valueOfPool=String.valueOf(pool.getValue());
}
});
return valueOfPool;
}
And what happens, is it goes through this code, returns valueOfPool right away without going through the onSuccess block and then goes through a 2nd time and enters on the onSuccess block. Since I return the value of pool to an activity that activity never gets the actual value.
get()
method is asynchronous which means the return statement will get executed before the onSuccessListener
, that's why you don't get the actual value. Therefore if you are using this value in another activity, then you can use Intent and inside the onSuccessListener
start the new activity:
Intent intent = new Intent(getBaseContext(), Activity.class);
intent.putExtra("value", valueOfPool);
startActivity(intent);
`