Search code examples
javaandroidandroid-intentsplash-screen

Android launch screen


I am building an Android application that opens different activities on start depending on firebase value. Right now I have an activity with just my logo that determines which activity to go to in its onCreate method. The problem is that I still get a white screen for half a second when launching my app because of the cold start. Is there a way to create a launch screen with my logo that will open a corresponding activity depending on returned firebase value and will replace the default white screen? Kinda like what WhatsApp or Instagram are doing these days. I know I can change window background in the styles.xml, but that's not ideal as it changes background everywhere and there will still be no way to determine which activity to open right on start


Solution

  • You need not change the background of all the windows. In order to show the splash screen instantaneously, consider using a custom theme just for the splash screen activity. Something like this:

    Styles.xml

    <style name="SplashTheme" parent="AppTheme">
        <item name="android:windowBackground">@drawable/splash_drawable</item>
    </style>
    

    Splash Activity Screen layout

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/splashLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:screenOrientation="portrait"
        android:theme="@style/SplashTheme">
    
        <ProgressBar
            android:id="@+id/splash_activity_progress"
            style="@style/Widget.AppCompat.ProgressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:indeterminate="true"/>
    </FrameLayout>
    

    This will give you a Splash screen with a drawable(app logo). The progress bar let the user know that there are some background operations in progress.

    To query the firebase you should use AsyncTask/Coroutine/Executor so that the operation is performed in background thread & the UI thread is not blocked. This is important to avoid ANRs.

    Considering the fact that there is a possibility of AsyncTask getting deprecated, I suggest you consider wither Kotlin coroutines/Java executors framework instead of AsyncTasks.