Search code examples
androidkotlinsplash-screenandroid-12

How to navigate user to specific screen based on authentication after splash screen API execution is completed in Android kotlin


I have integrated Android's new splash screen API into my app. Now I want to redirect the user to the login screen if the user is logged in or else to the registration screen if the user is not logged in once splash screen API execution is completed.

This is the theme that I have defined for the splash screen API

    <style name="Theme.CustomSplashScreen" parent="Theme.SplashScreen">
        <item name="windowSplashScreenAnimatedIcon">@drawable/splash_logo</item>
        <item name="windowSplashScreenAnimationDuration">5000</item>
        <item name="postSplashScreenTheme">@style/Theme.SanskarEducation</item>
    </style>

Here is the code from the activity

    @RequiresApi(Build.VERSION_CODES.S)
    override fun onCreate(savedInstanceState: Bundle?) {
        val splash = installSplashScreen()
        splash.setKeepOnScreenCondition { false }
        setupSplashScreen()
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_login)
        initialSetup()
    }

    @RequiresApi(Build.VERSION_CODES.S)
    private fun setupSplashScreen() {
        splashScreen.setOnExitAnimationListener { splashScreenView ->
            val slideUp = ObjectAnimator.ofFloat(
                splashScreenView,
                View.TRANSLATION_Y,
                0f,
                -splashScreenView.height.toFloat()
            )
            slideUp.interpolator = AnticipateInterpolator()
            slideUp.duration = 800L
            slideUp.doOnEnd { splashScreenView.remove() }
            slideUp.start()
        }
    }

I have also tried using the following code but it didn't work. It always redirect to login screen only.

val splash = installSplashScreen()
splash.setKeepOnScreenCondition { false }

I wanted to know where can I put my condition to check if the user is logged in or not so that I can redirect the user to a specific screen.

Thank you in advance


Solution

  • You can do that in onCreate of this Launcher activity. So while user see the splash screen you can make API call and decide which activity to open.

    @RequiresApi(Build.VERSION_CODES.S)
    override fun onCreate(savedInstanceState: Bundle?) {
        navigateToCorrectScreen()
        val splash = installSplashScreen()
        splash.setKeepOnScreenCondition { false }
        setupSplashScreen()
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_login)
        initialSetup()
    }
    

    and in navigateToCorrectScreen you can make api call and decide the activity to open like,

    private fun navigateToCorrectScreen() {
        val isLoggedIn = false // Implement your login status check here
        val targetActivity = if (isLoggedIn) {
            // User is logged in, redirect to the main activity
            MainActivity::class.java
        } else {
            // User is not logged in, redirect to the login activity
            LoginActivity::class.java
        }
        val intent = Intent(this, targetActivity)
        startActivity(intent)
        finish() // Finish the splash screen activity
    }
    

    Now if you think that there might be case where splash screen is disappeared but API response not received in that case you can manage it in multiple ways. Some are the ways listed below.

    1. You can check if you can remove splash screen only after API response
    2. You can show the progress or icon same as splash to that activity while you receive the response and decide where to go.