I understand this isn't particularly an issue, and I can't find anything online to help me with this.
I have a MainActivityclass that signs the user in with Firebase and gets the current user there, and assigns it to a global static clientUser variable. I have a fragment in another class (that the MainActivity uses) that contains the FirebaseRecyclerViewand uses the clientUser object to get its firebase ID.
How would I add that user's firebase ID into a database reference so that my FirebaseRecyclerView loads up that particular user's groups from the database into the RecyclerView?
Basically, I get a null ptr exception at the initialization of mRef here because clientUserhas not been created yet, because Firebase didnt log in yet. Any help/advice would be appreciated!
DatabaseReference mRef = FirebaseDatabase.getInstance().getReference().child("users").child(clientUser.get_fb_uid()).child(groups);
groupRecyclerViewAdapter = new FirebaseRecyclerAdapter<Group, GroupHolder>(Group.class, R.layout.grouptask_item, GroupHolder.class, mRef) {
@Override
protected void populateViewHolder(GroupHolder viewHolder, Group group, int position) {
viewHolder.setCardGroupName(group.getGroupName());
viewHolder.setGroupMemberCount(group.getMembers().size());
viewHolder.setCardAuthorProfileImg(group.getAuthorProfilePictureUrl());
}
};
Never mind! After hours of experimenting I realized I can just add the following in my fragment class!
```
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.grouptask_item_list, container, false);
itemTouchHelper = new ItemTouchHelper(simpleCallbackItemTouchHelper);
Context context = view.getContext();
recyclerView = (RecyclerView) view;
recyclerView.setHasFixedSize(false);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
final DatabaseReference mRef = FirebaseDatabase.getInstance().getReference().child("users");
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
Log.d(TAG, "User signed in... Collecting FireBase currentUser info");
// Currently signed in
groupRecyclerViewAdapter = new FirebaseRecyclerAdapter<Group, GroupHolder>(Group.class, R.layout.grouptask_item, GroupHolder.class, mRef.child(user.getUid()).child("groups")) {
@Override
protected void populateViewHolder(GroupHolder viewHolder, Group group, int position) {
viewHolder.setCardGroupName(group.getGroupName());
viewHolder.setGroupMemberCount(group.getMembers().size());
viewHolder.setCardAuthorProfileImg(group.getAuthorProfilePictureUrl());
}
};
recyclerView.setAdapter(groupRecyclerViewAdapter);
} else {
Log.d(TAG, "User is currently signed out");
}
}
};
itemTouchHelper.attachToRecyclerView(recyclerView);
return view;
}
```
@Joroze You should probably wait for the user to sign in... Firebase Auth provides a nice callback for this:
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth auth) {
if (auth.getCurrentUser() == null) {
// Not signed in
} else {
// Signed in, init your Firebase recyclerview here
}
}
In your onCreate and onDestroy:
FirebaseAuth.getInstance().addAuthStateListener(this); // In onCreate
FirebaseAuth.getInstance().removeAuthStateListener(this); // In onDestroy
PS: You _really_ shouldn't be storing the uid in a static variable. That will be full of bugs and weirdness when Android tries to restore your activities from a saved state. Use FirebaseAuth.getInstance().getCurrentUser().getUid() insead.
@SUPERCILEX Hi Alex!
That's what I've exactly figured out to do! Thanks for the advice on how I shouldn't use the static clientUser variable. Definitely makes sense!
@Joroze Awesome, glad you figured it out! 馃槃 Just FYI, the FirebaseUser class also had some other nifty methods like getEmail and
getDisplayName which you'll probably find useful as you go along.
@SUPERCILEX I see you know Firebase pretty well! I'm fairly new to Android development and Firebase, but I'm trying to impress my professor with these API's for my term project! For some silly reason I thought it would be a good idea to use getCurrentUserand create a new clientUser(getDisplayName, getEmail, etc.); at first.
By the way, maybe I can get some more of your opinions on these two ideas I'm doing:
getCurrentUser.getUID() to write and identify users on my database?getCurrentUserin?Thanks again!!!
@Joroze I'm happy to help!
For your first idea, that's totally right. You'll want to identify users with getUid() because the uid will never change over the lifetime of the user. This means that if a user signs out and then back in again, you can retrieve all of their data using the uid from Firebase Auth (because it will always be the same for that specific user).
For your second idea, it depends. For example I have an activity that can only be opened by tapping a list item in my main activity. Since there will be no list items in the main activity if the user is not signed in, I can safely assume that there will always be a logged in user when the second activity is opened. However, if the user can have both the activity where they sign out and the one that requires them to be signed in open at the same time, you'll definitely want an onAuthStateChanged listener to know when to close the activity that requires a sign in. For example, this is a simplified version of my code inside the activity that requires a signed in user:
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth auth) {
if (auth.getCurrentUser() == null) finish(); // This activity requires a sign in so we need to close it if the user signs out
}
Hope that helps! 馃槃
@SUPERCILEX That does! Thank you so much for all the advice. You've saved me HOURS of trouble-shooting!
@Joroze You're welcome! 馃槃
@SUPERCILEX PS: I emailed you about another Firebase RecyclerView related question, (if you don't mind)!
Most helpful comment
@Joroze You should probably wait for the user to sign in... Firebase Auth provides a nice callback for this:
In your
onCreateandonDestroy:PS: You _really_ shouldn't be storing the
uidin a static variable. That will be full of bugs and weirdness when Android tries to restore your activities from a saved state. UseFirebaseAuth.getInstance().getCurrentUser().getUid()insead.