Mastering Firebase for Android Development
上QQ阅读APP看书,第一时间看更新

Profile updating

In any of the onboarding services, it is very important to allow the user to update profile-related details such as updating profile pictures, displaying names, and so on. To accomplish this in a FirebaseUser instance, we have a method called updateProfile which accepts a UserProfileChangeRequest object with updated information:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName("Vinisha Krishna")
.setPhotoUri(Uri.parse("https://example.com/vinisha-k-bengaluru/profile.jpg"))
.build();

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

This code is self-explanatory. In the first block, we will get the reference of the currently logged in users. Then we will make use of the UserProfileChangeRequest builder class to set the changes, and then we will pass the changed object to the FirebaseUser class.