Search code examples
androidfirebasefirebase-authenticationandroid-side-navigation

Can not update user Display name utill I log out and login again in firebase


I have a text view in my nav header which I want to update when user updates its display name. Profile gets updates successfully but textview in nav header does not get updated util i log out and login again.

This is oncreate method

firebaseAuth = FirebaseAuth.getInstance();
user = firebaseAuth.getCurrentUser();       
nvDrawer = (NavigationView) findViewById(R.id.nvView);

// Setup drawer view
setupDrawerContent(nvDrawer);

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestIdToken(getString(R.string.default_web_client_id))
        .requestEmail()
        .build();

mGoogleSignInClient = new GoogleApiClient.Builder(this)
        .enableAutoManage(this , new GoogleApiClient.OnConnectionFailedListener() {
            @Override
            public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

            }
        } /* OnConnectionFailedListener */)
        .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
        .build();
headerLayout = nvDrawer.inflateHeaderView(R.layout.nav_header);
name =  headerLayout.findViewById(R.id.dp_name);
user_email= headerLayout.findViewById(R.id.email);
FirebaseAuth.AuthStateListener mAuthListener;

if (user!=null) {
    user_email.setText(user.getEmail());
    name.setText(user.getDisplayName());
    Log.i("name", "Dp name is" + user.getDisplayName());
}

FirebaseAuth auth = FirebaseAuth.getInstance();
FirebaseAuth.AuthStateListener authListener = new FirebaseAuth.AuthStateListener() {
    @Override
    public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
        FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
        if (firebaseUser != null) {
            name.setText(user.getDisplayName());
        }
    }
};

This is how I am updating profile.

final String user_name = editText.getText().toString();

if (TextUtils.isEmpty(user_name)) {
    editText.setError("This field is required");
    editText.requestFocus();
    Toast.makeText(getApplicationContext(), "Empty", Toast.LENGTH_LONG).show();
} else {
    UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
            .setDisplayName(user_name)
            .build();

    user.updateProfile(profileUpdates).addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
            if (task.isSuccessful()) {
                name.setText(user.getDisplayName());

                // Here task is successful but log does not return updated name
                Log.i("Updated", "Dp name is" +user.getDisplayName());
                Toast.makeText(getApplicationContext(), "Profile updated", Toast.LENGTH_LONG).show();
            } else Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_LONG).show();
        }
    });
}

Please see log. After successfully update of profile also I get old name in log.

Check in code. I have written a comment.


Solution

  • This is just a known issue in firebase. If you are using old firebaseAuth. Update your grade to 11.0.4