E-mail registered users displayName is not displayed in the Navigation Header after first login (Firebase)

Viewed 172

When a user logs in for the first time in my app he gets to a "Welcome screen" where he types in a username. After clicking on a button he gets to the MainActivity where in the NavigationDrawer should be his created username. The problem is that the username is only displayed after he logs out and in again.

The displayName is also displayed in another TextView in another activity where it is displayed in the correct way. In that activity I use this code:

// This code works as expected

namePlayer1 = view.findViewById(R.id.name_player_1);
        FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
        String userName =user.getDisplayName();
        namePlayer1.setText(userName);

So if you compare this with the code below you see that the problem only occurs when using a NavigationDrawer.

I know that this has been a bug in the Firebase Android SDK v9.8.0 but this should have been resolved by now right?

Here is the question I am referring to: displayName showing null after creating user with Firebase Auth

Here is my MainActivity which has a Navigation Drawer:

...

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);
            DrawerLayout drawer = findViewById(R.id.drawer_layout);
            NavigationView navigationView = findViewById(R.id.nav_view);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            drawer.addDrawerListener(toggle);
            toggle.syncState();
            navigationView.setNavigationItemSelectedListener(this);

            // Displays the username in the nav_header
            FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
            String userName = user.getDisplayName();
            View header = navigationView.getHeaderView(0);
            TextView text = header.findViewById(R.id.username);
            text.setText(userName);

...
2 Answers

Chances are it's just a racing problem. You sign the user up, but move too fast to the next activity before the user's details are registered.

Try to listen to the registration:

currentUser.updateProfile(profileUpdates)
             .addOnCompleteListener(new OnCompleteListener<Void>()
             {
                 @Override
                 public void onComplete(@NonNull Task<Void> task)
                 {
                     if (task.isSuccessful())
                     {
                         //TODO: move to MainActivity
                         Log.d(TAG, "User profile updated.");
                     }
                 }
         });

or simply pass the name to the MainActivity using

Intent intent = new Intent(this, MainActivity.class)
intent.putExtra("username", username);
startActivity(intent)

Try reloading user after you set user's display name before moving to the next activity.

FirebaseAuth.getInstance().getCurrentUser().reload().addOnCompleteListener(activity, new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                // goToNextActivity
            }
        });
Related