android-activity - 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();
}
解决方案
发生此错误是因为在进入下一个屏幕之前未关闭 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);
}
}
});
}
}
});
}
我希望这可以帮助你
推荐阅读
- spring-boot - 使用规范和谓词从数据库中检索数据的条件查询
- javascript - 从集合 firebase angular/typescript 中获取所有文档
- javascript - 如何将动画 SVG 转换为 React 功能组件
- amazon-web-services - org.apache.hadoop.fs.FileAlreadyExistsException:无法为路径创建目录:“s3a//bucket1/folder1”,因为它是一个文件
- django - 如何更新 django 中的特定多线程字段
- json - 使用 VBA 输出数组以 excel
- python-3.x - 在 Prefect 中,是否可以在流程运行期间缓存任务值?
- java - 使用 progaurd 混淆 JNA 结构时出现 getFieldOrder() 异常
- c# - 如何使用 IN() 搜索存在?
- powershell - 命令行什么都不显示没有输出