您如何进行公开课程以检索用户信息

我正在尝试使用一些方法来获取用户信息的类。我像图书馆一样。

例如,当前登录的用户在Firestore中有一些数据。说我要检索他的用户名。

这放置在我要检索用户名的同一活动中。

String currentUserUsername;

private String getUserUsername(DocumentReference currentUserDocument) {

    currentUserDocument.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {

            currentUserUsername = documentSnapshot.getString("username");

        }
    });

    return currentUserUsername;

}

And I'm calling getUserUsername(currentUserDocument) after activity onCreate so I can use currentUserUsername anywhere I want in the code. But the problem is that I'm doing this in many activities. How can I make it like a public class like those libraries?

评论