TheGMX
TheGMX

Reputation: 153

Blocking access to splash screen on Android

I've created a splash screen to my android app, it counts five seconds and then call my main activity,the actual problem is that if the users press the back button from the navigation bar the application will get back to the splash and will get stuck on that, and will just work again if totally close it and open again. How can i destroy the splash screen after getting into the main activity, or prevent if the user to get back? Also, is there anyway of ignoring the user taps during the splash screen?

Upvotes: 0

Views: 603

Answers (1)

Voicu
Voicu

Reputation: 17870

No need to block access to your spash activity. Just finish it before going to your main activity.

The full code of delaying and finishing the splash activity could be:

@Override
protected void onResume() {
    super.onResume();

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            // finish the splash activity so it can't be returned to
            SplashActivity.this.finish();
            // create an Intent that will start the second activity
            Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
            SplashActivity.this.startActivity(mainIntent);
        }
    }, 5000); // 5000 milliseconds
}

An alternative to finishing your splash activity is to define your activity in the manifest file with the android:noHistory attribute set to true:

<activity android:name=".SplashScreen" android:noHistory="true" ... />

As far as disabling touch events in an activity, this question can help.

Upvotes: 1

Related Questions