在Firebase中更改DisplayName

时间:2016-12-12 17:01:32

标签: android firebase firebase-authentication

我正在使用电子邮件注册Firebase。截至目前,他们无法在Firebase中设置DisplayName。所以我将用户重定向到一个类,他们可以在通过电子邮件和密码注册后立即更改DisplayName。

我的问题是我无法实现这一目标。这就是我到目前为止所做的事情:

public class UsernameActivity extends AppCompatActivity {
private FirebaseAuth.AuthStateListener authListener;
private FirebaseAuth auth;
private EditText mName;
private TextView btnSignUp;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_username);
    //get firebase auth instance
    auth = FirebaseAuth.getInstance();
    mName = (EditText) findViewById(R.id.name);

    btnSignUp = (TextView) findViewById(R.id.sign_up_button);
    final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    btnSignUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            auth = new FirebaseAuth.AuthStateListener() {
                @Override
                public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                    FirebaseUser user = firebaseAuth.getCurrentUser();
                }

                if(user!=null)

                {
                    UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
                            .setDisplayName(mName)
                            .build();
                    user.updateProfile(profileUpdates);
                    Intent intent = new Intent(UsernameActivity.this, JokeActivity.class);
                    startActivity(intent);
                }
            };
        }
    });
}}

以下是我想要实现的目标。

EditText

1 个答案:

答案 0 :(得分:0)

您必须遵循此选项

@Override
protected void onStart() {
    super.onStart();
    firebaseAuth.addAuthStateListener(mAuthListener); //firebaseAuth is of class FirebaseAuth
}

@Override
protected void onStop() {
    super.onStop();
    if (mAuthListener != null) {
        firebaseAuth.removeAuthStateListener(mAuthListener);
    }
}

并在你的OnCreate()或onCreateView()中显示:

firebaseAuth = FirebaseAuth.getInstance();
    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                if (!LoginFragment.displayName.isEmpty()) {
                    UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
                            .setDisplayName(LoginFragment.displayName).build();
                    user.updateProfile(profileUpdates).addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if (task.isSuccessful()) {
                                Log.d("Display name: ", FirebaseAuth.getInstance().getCurrentUser().getDisplayName());
                            }
                        }
                    });
                }
                LoginFragment.displayName = "";
            }
        }
    };