问题描述
我正在使用电子邮件注册Firebase。截至目前,他们无法在Firebase中设置DisplayName。所以我将用户重定向到一个类,他们可以通过电子邮件和密码注册后立即更改DisplayName。我的问题是我无法做到这一点。这是我迄今为止做的:
pre $ public class UsernameActivity extends AppCompatActivity {
private FirebaseAuth.AuthStateListener authListener;
私人FirebaseAuth验证;
private EditText mName;
私人TextView btnSignUp;
@Override
protected void onCreate(Bundled savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_username);
//获取firebase auth实例
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();
}
$ b $ if(user!= null)
{
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName(mName)
.build();
user.updateProfile(profileUpdates);
intent = new Intent(UsernameActivity.this,JokeActivity.class);
startActivity(intent);
}
};
}
}) ;
}}
以下是我想实现的图像。
@Override
protected void onStart (){
super.onStart();
firebaseAuth.addAuthStateListener(mAuthListener); // firebaseAuth是类FirebaseAuth
$ b @Override
保护无效onStop(){
super.onStop();
if(mAuthListener!= null){
firebaseAuth.removeAuthStateListener(mAuthListener); ()或onCreateView()函数)中的一个或多个元素,并将其添加到您的OnCreate()或onCreateView()函数中这:
firebaseAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener(){
@Override $ b $ public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth){
FirebaseUser user = firebaseAuth.getCurrentUser();
if(user!= null){
if(!LoginFragment.displayName.isEmpty()){
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName(LoginFragment。显示名).build();
user.updateProfile(profileUpdates).addOnCompleteListener(new OnCompleteListener< Void>(){
@Override $ b $ public void onComplete(@NonNull Task< Void> task){
if (),FirebaseAuth.getInstance()。getCurrentUser()。getDisplayName());
}
}
(显示名称:)task.isSuccessful });
}
LoginFragment.displayName =;
}
}
};
I am using email registration Firebase. As of now their is no option to set DisplayName in Firebase. So I am redirecting users to a class where they can change DisplayName soon after signing up through email and password.
My problem is I am unable to achieve that. Here's what I have done so far:
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);
}
};
}
});
}}
Below is the image of what I want to achieve.
解决方案 You have to follow this option
@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);
}
}
and in your OnCreate() or onCreateView() implment this:
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 = "";
}
}
};
这篇关于更改Firebase中的DisplayName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!