简体   繁体   中英

Displaying SplashScreen while user waits authentication from FirebaseAuth

I'm trying to display a splash screen on my App and I've managed to do it using a Handler and a Runnable object that constructs an intent and after 2 seconds changes activities to my login activity using the intent constructed

However, now I've set up the Firebase Authentication. I've tried to construct the intent to match the expected behavior:
(user signedIn --> show SplashScreen --> Skip LoginActivity)
(user notSignedIn --> show SplashScreen --> Show LoginActivity)
Here's the code I'm currently working on:

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);

    }
}

I've ommited the imports to shorten the code. The problem is that the app stay indefinetely in the splashscreen.

The issue is that you are using an AuthStateListener. You really just need to do the following without using an 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));
                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM