2

I have a splash screen which is being showed only on fresh start of app. If user hit back button and start app again the splash doesn't show. Everything is fine until here, if splash doesn't show, there is a 1-2 second black screen when opening app. Here is my splashactivity java file;

public class SplashScreen extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("first_time", false)) // if first time, show splash
{

    SharedPreferences.Editor editor = prefs.edit();
    editor.putBoolean("first_time", true);
    editor.commit();


    setContentView(R.layout.activity_splash);


    Thread t = new Thread() {
        public void run() {
            try {
                int time = 0;
                while (time < 4000) {
                    sleep(100);
                    time += 100;
                }
            }
            catch (InterruptedException e) {
                // do nothing
            }
            finally {

                Intent i = new Intent(SplashScreen.this, MainActivity.class);
                startActivity(i);
                finish();
            }
        }
    };
    t.start();
}
else // if not first time, dont show splash
{
    setContentView(R.layout.activity_splash);
    Intent i = new Intent(SplashScreen.this, MainActivity.class);
    startActivity(i);
    finish();
}

How can I fix this issue ?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
user198989
  • 4,574
  • 19
  • 66
  • 95

5 Answers5

2

Adding @Bryan I recommend you the cold start. For Example i did like this

In your Style.xml:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

<style name="AppTheme.splashScreenLauncher">
    <item name="android:windowBackground">@drawable/splash_screen</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>

</style>

The splash_screen.xml:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">
    <!-- The background color, preferably the same as your normal theme -->
    <item android:drawable="@android:color/white"/>

    <item>
        <bitmap
            android:src="@drawable/SPLASHIMAGE"
            android:gravity="center"/>
    </item>
</layer-list>

In the manifest, where you set your launcher activity:

android:theme="@style/AppTheme.splashScreenLauncher"> 

In your launcher activity:

setTheme(R.style.AppTheme); // IMPORTANT BEFORE super.onCreate
super.onCreate(savedInstanceState);
MatiRC
  • 189
  • 1
  • 3
  • 13
1

Add this in app styles:

<item name="android:windowDisablePreview">true</item>
Bruno Ferreira
  • 1,561
  • 1
  • 10
  • 17
1

Because you are checking if it is the "first time" opening the app before showing the splash screen:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("first_time", false)) {
    // Show splash
} else {
    // Don't show splash
}

This is not the right way to create a splash screen in the first place. You shouldn't make your users wait for the splash screen by creating a timer. Instead, you should only show the splash screen for as long as it takes your app to load.

To do this you should create a simple layer-list drawable to use as the background of your splash activity:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- A solid background fill -->
    <item
        android:drawable="@color/gray"/>
    <item>

    <!-- A centered logo -->
    <bitmap
        android:gravity="center"
        android:src="@mipmap/ic_launcher"/>
    </item>

</layer-list>

Then use this layer-list as the background for the theme you will use in your splash activity:

<resources>

    <!-- Base application theme -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>

    <!-- Splash Screen theme -->
    <style name="SplashTheme">
        <item name="android:background">@drawable/background_splash</item>
        <item name="android:windowAnimationStyle">@null</item>
    </style>

</resources>

Apply that theme to your splash activity in the manifest:

<activity android:name=".SplashScreen"
    android:theme="@style/SplashTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

Then, finally, all you have to do in your splash activity is start your MainActivity:

public class SplashScreen extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}

Make sure you don't call setContentView() in your splash activity, as this will add unnecessary loading time to your splash screen.

Bryan
  • 14,756
  • 10
  • 70
  • 125
  • Better approach, but this will show splash everytime user opens app ? Lets say my app loaded on 1 second and this will show splash only 1 second, isnt that unnecessary ? How about using this technique and showing splash minumum 3 seconds and only on fresh start ? – user198989 Aug 02 '17 at 13:41
  • @user198989 Because that is not best practice, as mentioned in the [article](https://www.bignerdranch.com/blog/splash-screens-the-right-way/) I linked to. Why make the user wait? Isn't the purpose of a mobile app to be fast? – Bryan Aug 02 '17 at 13:54
  • @user19889 And yes, this method will show the splash screen every time the app is opened (but not when it is brought to the front after being suspended). – Bryan Aug 02 '17 at 13:59
1
 /Just use the below code Snipeet/

Add following code in Style.xml file

<style name="Theme.Transparent" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowIsTranslucent">true</item>

</style>

 And then use this style in AndroidManifest inside application Tag
 Like this: 
 android:theme="@style/Theme.Transparent"
yash786
  • 1,151
  • 8
  • 18
1

You have to give splash screen background colour as your application main theme colour.

<activity
        android:name=".SplashScreen"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/SplashScreenTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Set style In Your values/style.xml `

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<style name="SplashScreenTheme" parent="AppBaseTheme">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowBackground">@color/white</item>
    <item name="colorPrimaryDark">@color/white</item>
</style>

Set style in your values-v21/style.xml

 <style name="SplashScreenTheme" parent="AppBaseTheme">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowBackground">@color/white</item>
    <item name="android:colorPrimaryDark">@color/white</item>
</style>
Rana.S
  • 2,265
  • 3
  • 20
  • 19