0
votes

When I open the app, the splash is shown and then there is a black screen (for just 1 second), and then goes to the activity. How can I avoid this black screen?

I set the splash as a theme.

class SplashActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (sharedPrefs.isUserLogged) {
            startActivity(intentFor<MainActivity>().clearTask().newTask())
        }else{
            startActivity(intentFor<LoginActivity>().clearTask().newTask())
        }
        finish()
    }
}

I set the splash style on my AndroidManifest.xml

<activity android:name=".view.splash.SplashActivity"
                  android:theme="@style/AppTheme.Launch">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
</activity>

and in my style, I set a drawable for the background.

<style name="AppTheme.Launch">
    <item name="android:windowBackground">@drawable/my_splash</item>
</style>
1
tried <item name="android:windowDisablePreview">true</item>? - Shweta Chauhan
Why don't you set drawable in the splash activity layout? Maybe changing styles causes delay. - underoid
@ShwetaChauhan yes, but this skip the splash - yasin
@underoid I was following this tutorial android.jlelse.eu/… If I set the drawable in the layout, there is a white screen before the splash and no black screen. - yasin
Try setting the same style for Main and Login activities and make sure their layout covers whole screen. I guess this way while second activity creates you will continue to see splash drawable instead of black screen. - underoid

1 Answers

0
votes

I found a solution after a while:

class SplashActivity : AppCompatActivity() {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    Timer().schedule(1000) {
       if (sharedPrefs.isUserLogged) {
           startActivity(intentFor<MainActivity>().clearTask().newTask())
       }else{
           startActivity(intentFor<LoginActivity>().clearTask().newTask())
       }
    }
  }
}

and I set the theme like this:

<style name="AppTheme.Launch">
    <item name="android:windowBackground">@drawable/my_splash</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>