Search code examples
javaandroidandroid-intentandroid-activitysplash-screen

Android app opens after a few seconds after pressing home button during splash screen


I am having an issue I can't seem to figure out the reason for.

When you launch the app, a splash screen is first displayed for 2.5 seconds before finishing and starting a new activity. If you press the home or back button during this time the app will close as normal. However after a few seconds (longer than 2.5) the app will open and start from the activity that comes after the splash screen.

Any help on why this happens is appreciated!

Here is the implementation of the Splash screen (I do however not believe anything here causes this issue as I've tried different implementations)

`public class SplashScreenActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_screen);

    Thread myThread = new Thread(){
        @Override
        public void run() {
            try {
                sleep(2500);
                Intent intent = new Intent(getApplicationContext(),MainActivity.class);
                startActivity(intent);
                finish();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    myThread.start();` 

Here's the manifest

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.VIBRATE" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".activities.MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar"
        android:launchMode = "singleInstance">
    </activity>
    <activity android:name=".activities.SplashScreenActivity"
        android:theme="@style/Theme.AppCompat.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".alert.BroadCaster" >
    </receiver>
    <service android:name=".timer.TimerService"
        android:process=":timerservice" />
</application>


Solution

  • It happens because you are creating a new Thread and this thread will be still alive after you put your app in background. You can change your approach using an Handler. If you need that your next Activity won't start if the splash screen is in background, you have to store the current time before the delay starts.

    private static final long SPLASH_SCREEN_MS = 2500;
    
    private long mTimeBeforeDelay;
    private Handler mSplashHandler;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);
        // Create a new Handler.
        mSplashHandler = new Handler();
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        // The first time mTimeBeforeDelay will be 0.
        long gapTime = System.currentTimeMillis() - mTimeBeforeDelay;
        if (gapTime > SPLASH_SCREEN_MS) {
            gapTime = SPLASH_SCREEN_MS;
        }
        mSplashHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
                startActivity(intent);
                SplashScreenActivity.this.finish();
            }
        }, gapTime);
        // Save the time before the delay.
        mTimeBeforeDelay = System.currentTimeMillis();
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        mSplashHandler.removeCallbacksAndMessages(null);
    }