gpt4 book ai didi

java - 在用户等待来自 FirebaseAuth 的身份验证时显示 SplashScreen

转载 作者:行者123 更新时间:2023-11-30 00:06:47 25 4
gpt4 key购买 nike

我正在尝试在我的应用程序上显示一个启动画面,我已经设法使用 Handler 和一个构建 Intent 的 Runnable 对象来完成它,并在 2 秒后使用构建的 Intent 将 Activity 更改为我的登录 Activity

但是,现在我已经设置了 Firebase 身份验证。我试图构建与预期行为相匹配的 Intent :
(用户登录 --> 显示 SplashScreen --> 跳过 LoginActivity)
(用户 notSignedIn --> 显示 SplashScreen --> 显示 LoginActivity)
这是我目前正在处理的代码:

public class SplashScreenActivity extends AppCompatActivity {


private FirebaseAuth mAuth = null;
private FirebaseAuth.AuthStateListener mAuthListener;

/** Duration of wait **/
private final int SPLASH_DISPLAY_LENGTH = 1000;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_splash_screen);

new Handler().postDelayed(new Runnable(){
@Override
public void run() {
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {

FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in, send to mainmenu
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
startActivity(new Intent(SplashScreenActivity.this, MainMenuActivity.class));
} else {
// User is signed out, send to register/login
startActivity(new Intent(SplashScreenActivity.this, MainActivity.class));
}
}
};
}
}, SPLASH_DISPLAY_LENGTH);

}
}

我省略了导入以缩短代码。问题是该应用程序不确定地停留在启动画面中。

最佳答案

问题是您使用的是 AuthStateListener。您实际上只需要在不使用 AuthStateListener 的情况下执行以下操作:

             FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in, send to mainmenu
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
startActivity(new Intent(SplashScreenActivity.this, MainMenuActivity.class));
} else {
// User is signed out, send to register/login
startActivity(new Intent(SplashScreenActivity.this, MainActivity.class));
}

关于java - 在用户等待来自 FirebaseAuth 的身份验证时显示 SplashScreen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48829890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com