Firebase更新成功后,应用中的DisplayName不会更改 - Android

时间:2017-05-17 01:30:39

标签: android firebase firebase-authentication

标题可能有点令人困惑,所以让我在这里做一些前言。我正在开发一款允许用户编辑个人资料信息的Android应用。当用户点击HomePage中的按钮去编辑他们的信息,并且他们成功更新信息并被重新导航到HomePage时,我就停留在那里,直到用户才会显示新更新的信息注销并重新登录。我希望该应用程序比用户更友好。

这是我的相关代码:

HomePage.java:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_page);

    mTextMessage = (TextView) findViewById(R.id.message);
    isVerified = (TextView) findViewById(R.id.activity_home_page_is_email_verified);
    verifyButton = (Button) findViewById(R.id.verify_email_button);
    signOutButton = (Button) findViewById(R.id.sign_out_button);
    completeInfoButton = (Button) findViewById(R.id.complete_info_button);
    homePage = this;

    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    user = FirebaseAuth.getInstance().getCurrentUser();
    mTextMessage.setText("Welcome " + user.getEmail());
    if (user.isEmailVerified()){
        isVerified.setText("Email is verified");
    }
    else{
        isVerified.setText("Email is not verified");
    }

completeInfoButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            Intent intent = new Intent(HomePage.this, CompleteProfileActivity.class);
            startActivity(intent);
        }
    });

CompleteProfileActivity.java:

private String TAG = "UpdateProfile: ";
private FirebaseUser user;
private EditText username, email;
private ImageView profilePhoto, successCheck;
private Button saveDataButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_complete_profile);

    user = FirebaseAuth.getInstance().getCurrentUser();
    username = (EditText) findViewById(R.id.complete_profile_username);
    email = (EditText) findViewById(R.id.complete_profile_email);
    //ADD PROFILE PHOTO
    saveDataButton = (Button) findViewById(R.id.complete_profile_save);
    successCheck = (ImageView) findViewById(R.id.complete_profile_success_check);

    email.setText(user.getEmail());
    successCheck.setVisibility(View.GONE);
    if(user.getDisplayName() != null){
        username.setText(user.getDisplayName());
    }
    else{
        username.setHint("Enter your name");
    }

    saveDataButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            UserProfileChangeRequest saveUpdates = new UserProfileChangeRequest.Builder()
                    .setDisplayName(username.getText().toString())
                    .build();

            user.updateProfile(saveUpdates)
                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if (task.isSuccessful()){
                                HomePage.homePage.finish();
                                Log.d(TAG, "User profile updated");
                                successCheck.setVisibility(View.VISIBLE);

                                Timer t = new Timer();
                                t.schedule(new TimerTask() {
                                    @Override
                                    public void run() {
                                        Intent intent = new Intent(CompleteProfileActivity.this, HomePage.class);
                                        startActivity(intent);
                                        finish();
                                    }
                                }, 3000);
                            }
                            else{
                                Log.d(TAG, "User profile not updated");
                                Intent intent = new Intent(CompleteProfileActivity.this, HomePage.class);
                                startActivity(intent);
                                finish();
                            }
                        }
                    });

        }
    });
}

0 个答案:

没有答案