I am working on developing one Android application and I have decided that I want to save the data in Firebase. Currently I have one obstacle with loading the data. First off all I would like to show the architecture of application:
In method OnCreate
is created the instance of FirebaseHandler (it is singleton class). After the instance exist, MainActivity call method nameOfMember.
public void nameOfMember() {
DocumentReference documentReference = db.document(pathTOOffersPrefix + userID);
Log.d(TAG, "nameOfMember: " + pathTOOffersPrefix + userID);
documentReference.get()
.addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists()) {
String name = documentSnapshot.getString(ARG_Firebase_User_Name);
String surname = documentSnapshot.getString(ARG_Firebase_User_SurName);
setName(name);
setSurname(name);
Log.d(TAG, "onSuccess: " + name + " " + surname);
} else {
Log.d(TAG, "onSuccess: but not exist");
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "onFailure: " + e.getMessage());
}
});
}
This method setUp two argument of FirebaseHandler instance (for future using). It is working but now come the problem. In Main.java I try to get this argument:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firebaseHandler = FirebaseHandler.getInstance();
firebaseHandler.getName();
firebaseHandler.getSurname();
}
The Main problem is, that the method Main.onCreate will be finisihed earlier than FirebaseHandler.onSuccess.
I would like to ask, if anybody know how to solve it (some callback? or).