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

I have a problem, I cannot figure out the reason.

When you launch the app, the splash screen is first displayed 2.5 seconds before the new activity finishes and starts. If you press the Home or Back button during this time, the application will close as usual. However, after a few seconds (longer than 2.5) the app will open and start with the activity that appears after the splash screen.

Any help on why this is happening is appreciated!

Here is the Splash screen implementation (I still don't believe anything here is causing this problem 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>

      

+3


source to share


3 answers


This is because you are creating a new Thread

one and this thread will stay alive after you put your application in the background. You can change your approach using Handler

. If you need your next one Activity

not to start while the screensaver is in the background, you must keep 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);
}

      

+2


source


Just use a handler instead of a sleep thread like this



new Handler().postDelayed(new Runnable(){
            @Override
            public void run() {
                Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
                startActivity(intent);
                SplashScreenActivity.this.finish();
            }
        }, SPLASH_DURATION);

      

+2


source


You only need to implement the method onStop()

if you want to save data and memory.

0


source







All Articles