You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android应用注册布局显示异常与OTP验证跳转故障求助

Alright, let's dig into your issues and fix them up. I've spotted a few key problems in your code that are causing these weird behaviors.

问题根源分析与修复方案

1. 核心致命错误:AuthStateListener生命周期管理混乱

Your onStop method has a critical mistake — you're adding an AuthStateListener instead of removing it! This causes multiple listeners to pile up every time the activity stops, leading to conflicting auth state callbacks that mess up all your navigation and layout display logic.

Fix the onStop method:

@Override
protected void onStop() {
    if (firebaseAuth != null && listener != null)
        firebaseAuth.removeAuthStateListener(listener); // Change add to remove!
    super.onStop();
}

Also, optimize the listener addition in delaySplashScreen to avoid duplicates:

private void delaySplashScreen() {
    progress_bar.setVisibility(View.VISIBLE);
    Completable.timer(3, TimeUnit.SECONDS, AndroidSchedulers.mainThread())
            .subscribe(() -> {
                // Ensure only one listener is active at a time
                if(firebaseAuth != null && listener != null) {
                    firebaseAuth.removeAuthStateListener(listener);
                }
                firebaseAuth.addAuthStateListener(listener);
            });
}

2. OTP Verification Jump Issue: Missing Active User Check

After OTP verification succeeds, you only fetch the FirebaseUser in onActivityResult but don't trigger the user data check flow. Relying on the AuthStateListener callback might fail due to the earlier listener pileup, leading you back to the login screen instead of registration.

Fix the onActivityResult method:

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == LOGIN_REQUEST_CODE) {
        IdpResponse response = IdpResponse.fromResultIntent(data);
        if(resultCode == RESULT_OK) {
            FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
            if(user != null) {
                // Proactively check if user exists in database to trigger registration/home flow
                checkUserFromFirebase();
            }
        } else {
            // Add null check to avoid crashes when user cancels login
            if(response != null) {
                Toast.makeText(this, response.getError().getMessage(), Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "Login cancelled", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

3. Register Layout Not Showing First Time: Fixed by Previous Fixes

Once you fix the listener lifecycle and add the active user check, this problem will resolve automatically. After OTP verification, checkUserFromFirebase will run, detect no existing user data in the database, and correctly show the registration AlertDialog.

Extra Optimization Tips

  • FirebaseInstanceId.getInstance().getInstanceId() is deprecated. Replace it with FirebaseMessaging.getInstance().getToken() to get your push notification token:
    FirebaseMessaging.getInstance().getToken()
            .addOnFailureListener(e -> Toast.makeText(SplashScreenActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show())
            .addOnSuccessListener(token -> {
                Log.d("TOKEN", token);
                UserUtils.updateToken(SplashScreenActivity.this, token);
            });
    
  • In showRegisterLayout, add dialog.setCancelable(false) to prevent users from accidentally closing the registration popup mid-flow.

After applying these fixes, test your flow:

  1. First launch: Splash screen → Login screen after 3 seconds
  2. OTP verification: Login success → Registration layout pops up immediately
  3. Complete registration → Navigates to home screen
  4. Restart app: Splash screen → Directly to home (if registered) or registration layout (if not)

This should resolve all your reported issues!

内容的提问来源于stack exchange,提问作者amiya prakash

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:43:40