首页 > 解决方案 > startup.ActivityLogin 泄露了最初添加在这里的窗口 DecorView@3a9e526[ActivityLogin]

问题描述

这是我的代码,我试图允许用户通过他的 Gmail id 登录。但我收到此错误: 错误图像

不知道我错过了什么,我想我在正确的地方调用了dismiss()。是时间问题吗?不过,该应用程序在我的手机中运行良好。我不确定它是否会在其他设备中崩溃,所以我想摆脱这个错误。

    //Google Sign In
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == GOOGLE_SIGN_IN_KEY) {
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        try {
            GoogleSignInAccount account = task.getResult(ApiException.class);
            firebaseAuthWithGoogle(account);

        } catch (ApiException e) {
            if (e.getStatusCode() == 12500) {
                Snackbar.make(findViewById(android.R.id.content), "Sign In Error! Update Google Play Service.", Snackbar.LENGTH_LONG).show();
            }
        }
    }
}

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    firebaseAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        FirebaseUser user = firebaseAuth.getCurrentUser();
                        showGoogleSignUpDialog();
                        storeUserInfo(user.getPhotoUrl().toString(), user.getUid(), user.getDisplayName(), user.getEmail());
                    } else {
                        Snackbar.make(findViewById(android.R.id.content), "Sign In Failed!", Snackbar.LENGTH_LONG).show();
                    }
                }
            });
}

private void storeUserInfo(final String stringUserImage, final String stringUserID, final String stringName, final String stringEmail) {
    FirebaseUser user = firebaseAuth.getCurrentUser();

    UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
            .setDisplayName(stringName)
            .setPhotoUri(Uri.parse(stringUserImage))
            .build();

    user.updateProfile(profileUpdates)
            .addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if (task.isSuccessful()) {
                        firebaseFirestore.collection("UserData").document(stringUserID).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                            @Override
                            public void onSuccess(DocumentSnapshot documentSnapshot) {
                                if (documentSnapshot.getData() != null && documentSnapshot.getData().size() > 0) {
                                    Snackbar.make(findViewById(android.R.id.content), "Welcome back!", Snackbar.LENGTH_LONG).show();
                                    startActivity(new Intent(ActivityLogin.this, ActivityHome.class));
                                    finish();

                                } else {
                                    createUserData(stringUserID, stringUserImage, stringName, stringEmail);
                                }
                            }
                        });
                    }
                }
            });
}

private void createUserData(String stringUserID, String stringUserImage, String stringName, String stringEmail) {
    final Map<String, Object> userDataMap = new HashMap<>();
    userDataMap.put("userName", stringName);
    userDataMap.put("userImage", stringUserImage);
    userDataMap.put("userID", stringUserID);
    userDataMap.put("userPoints", 100);
    userDataMap.put("userVerified", true);
    userDataMap.put("userEmail", stringEmail);
    userDataMap.put("timestamp", FieldValue.serverTimestamp());

    firebaseFirestore.collection("UserData").document(stringUserID).set(userDataMap)
            .addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            alertGoogleSignIn.dismiss();
                            Snackbar.make(findViewById(android.R.id.content), "Welcome!", Snackbar.LENGTH_LONG).show();
                            startActivity(new Intent(ActivityLogin.this, ActivityHome.class).putExtra("canShowCoinCredit", true));
                            finish();
                        }
                    }, 3000);
                }
            });
}

private void showGoogleSignUpDialog() {
    final AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Signing in");
    alert.setMessage("Please hold on while we process...");
    alert.setCancelable(false);
    alertGoogleSignIn = alert.create();
    alertGoogleSignIn.show();
}

标签: android-activitydialogalert

解决方案


发生此错误是因为在进入下一个屏幕之前未关闭 alertGoogleSignIn 对话框

需要提前alertGoogleSignIn解散startIntent

请添加alertGoogleSignIn.dismiss()以下功能

 private void storeUserInfo(final String stringUserImage, final String stringUserID, final String stringName, final String stringEmail) {
        FirebaseUser user = firebaseAuth.getCurrentUser();

        UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
                .setDisplayName(stringName)
                .setPhotoUri(Uri.parse(stringUserImage))
                .build();

        user.updateProfile(profileUpdates)
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            firebaseFirestore.collection("UserData").document(stringUserID).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                                @Override
                                public void onSuccess(DocumentSnapshot documentSnapshot) {
alertGoogleSignIn.dismiss();  // dismiss dialog here                                  
if (documentSnapshot.getData() != null && documentSnapshot.getData().size() > 0) {
                                        Snackbar.make(findViewById(android.R.id.content), "Welcome back!", Snackbar.LENGTH_LONG).show();
                                        startActivity(new Intent(ActivityLogin.this, ActivityHome.class));
                                        finish();

                                    } else {
                                        createUserData(stringUserID, stringUserImage, stringName, stringEmail);
                                    }
                                }
                            });
                        }
                    }
                });
    }

我希望这可以帮助你


推荐阅读