'Fetch user data during splash screen and pass it to next activity
I am developing an Android app in which i need to retrieve data from Firestore during app's splash screen and use a particular value from that document snapshot to retrieve another document. For this I need to get the string value out of Firebase's get() function. But when i try this, I am not getting the value of the variable out of the function:
String userCityL;
DocumentReference docRef = firestoreDB.collection("users").document(mail);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
String userCityL;
if (document.exists()) {
Log.d(TAG, "DocumentSnapshot data: " + document.getData());
userNameL = document.getString("UserName");
userPhoneL = document.getString("UserPhoneNo");
userDegreeL = document.getString("UserDegree");
userSpecialL = document.getString("UserSpecial");
userCityL = document.getString("UserCity");
userProfilePicL = document.getString("downloadUri");
TextView userCity = findViewById(R.id.tvCity);
userCity.setText(userCityL);
//loadNotesList();
//dummy(userCityL);
//loadNotesList(userCityL);
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
Here, nothing is being stored in userCityL variable.
Solution 1:[1]
I solved this problem using "shared preferences". My problem is to fetch user data of the user who is currently logging in and use this data across my entire app in all the activities. Here i fetch the data from firebase during the splash screen and store it in shared preference file and use those data in other activities. Thanks :)
Solution 2:[2]
You are not getting the value of that String outside the onComplete() function not outside the get() function. This is happening because the onComplete() function has an asynchronous behavior. You also cannot simply create a global variable and use it's value outside the function because it will always be null. If you want to use a value that is coming from the database outside the onDataChange() method, you have two choices. The first one would be to pass the desired object as argument to a method that is defined in your class like this:
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
String userCityL = document.getString("UserCity");
methodThatDoesSomething(userCityL); //Method call
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
private void methodThatDoesSomething(String userCityL) {
//Do what you need to do with your userCityL object
}
Or, if you want it in a more complex way, then dive into the asynchronous world of modern API's and see the last part of my answer in this post, in which I have explained step by step how you can achieve this using a custom callback. For more informations, you can take also a look at this video.
Solution 3:[3]
It is, but the you are problably calling the string before getting the result from the fucntion.
Try and put the function where you call the string inside that function.
check Alex Mamo video where you have the explanation.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alex Mamo |
| Solution 2 | Alex Mamo |
| Solution 3 |
