Search code examples
androidsplash-screen

Splash screen not displaying in android


When i run the app, SplashScreen blinks for a fraction of second on the screen and disappears completely for 3 seconds. After completing 3 sec MainActivity launches. Question is how to display my splash screen with out disapearing?

public class SplashScreen extends Activity {
private static int SPLASH_TIME_OUT = 3000;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash_screen);

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            startActivity(new Intent(SplashScreen.this, MainActivity.class));

        }
    }, SPLASH_TIME_OUT);
    SplashScreen.this.finish();
}

@Override
public void onBackPressed() {
    SplashScreen.this.finish();
    super.onBackPressed();
}}

MANIFEST FILE

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name="com.simbotix.guardianonthego.SplashScreen" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.simbotix.guardianonthego.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

Solution

  • Change this

     new Handler().postDelayed(new Runnable() {
    
            @Override
            public void run() {
                startActivity(new Intent(SplashScreen.this, MainActivity.class));
    
            }
        }, SPLASH_TIME_OUT);
        SplashScreen.this.finish();
    

    to

    new Handler().postDelayed(new Runnable() {
    
            @Override
            public void run() {
                 startActivity(new Intent(SplashScreen.this, MainActivity.class));
                  SplashScreen.this.finish();
            }
        }, SPLASH_TIME_OUT);
    

    Also no need to finish Activity in onBackPressed

    @Override
    public void onBackPressed() {
        SplashScreen.this.finish(); // Remove this
        super.onBackPressed();
    }