How to remove black screen when opening app

user198989

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 ?

Bryan

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Black screen when opening app after a few hours of inactivity

From Dev

Screen Flashes/Flickers to Black When Opening PowerPoint 2010

From Dev

How to remove black screen on AppIcon - IOS

From Dev

How do I keep my Cordova app full-screen when opening the Android keyboard?

From Dev

Getting black screen when trying to remove ViewController from display in iOS

From Dev

Black screen when executing test app on Tizen Emulator

From Dev

Random black screen when launching android app on device

From Dev

Black screen when restart zbar barcode scanner sample app

From Dev

Screen goes black when changing activity in Android app

From Dev

Black screen when executing test app on Tizen Emulator

From Dev

Black screen when restart zbar barcode scanner sample app

From Dev

Screen goes black when changing activity in Android app

From Dev

getting black screen on windows when laucnh my kivy app

From Dev

How to remove black border when GUI is selected?

From Dev

How to remove the delay when opening an Activity with a DrawerLayout?

From Dev

How to remove message when opening terminal/solve?

From Dev

How to remove black bar menu from the bottom of android screen?

From Dev

How to remove black bar menu from the bottom of android screen?

From Dev

Swift : How to make a Black screen to guidelines for user to use app

From Dev

Black screen on launch - iOS app

From Dev

App starts with black screen in iPhone

From Dev

iOS App Goes to Black Screen

From Dev

Android app black screen ( onCreate() )

From Dev

App shows black screen in iOS

From Dev

EarlGrey launches APP with black screen

From Dev

How to show paused video instead of black screen when starting AVPlayer

From Dev

How to proceed after getting black screen when booting Ubuntu 20.04?

From Dev

Black Screen when Computer Boots

From Dev

Black screen when logging into desktop

Related Related

  1. 1

    Black screen when opening app after a few hours of inactivity

  2. 2

    Screen Flashes/Flickers to Black When Opening PowerPoint 2010

  3. 3

    How to remove black screen on AppIcon - IOS

  4. 4

    How do I keep my Cordova app full-screen when opening the Android keyboard?

  5. 5

    Getting black screen when trying to remove ViewController from display in iOS

  6. 6

    Black screen when executing test app on Tizen Emulator

  7. 7

    Random black screen when launching android app on device

  8. 8

    Black screen when restart zbar barcode scanner sample app

  9. 9

    Screen goes black when changing activity in Android app

  10. 10

    Black screen when executing test app on Tizen Emulator

  11. 11

    Black screen when restart zbar barcode scanner sample app

  12. 12

    Screen goes black when changing activity in Android app

  13. 13

    getting black screen on windows when laucnh my kivy app

  14. 14

    How to remove black border when GUI is selected?

  15. 15

    How to remove the delay when opening an Activity with a DrawerLayout?

  16. 16

    How to remove message when opening terminal/solve?

  17. 17

    How to remove black bar menu from the bottom of android screen?

  18. 18

    How to remove black bar menu from the bottom of android screen?

  19. 19

    Swift : How to make a Black screen to guidelines for user to use app

  20. 20

    Black screen on launch - iOS app

  21. 21

    App starts with black screen in iPhone

  22. 22

    iOS App Goes to Black Screen

  23. 23

    Android app black screen ( onCreate() )

  24. 24

    App shows black screen in iOS

  25. 25

    EarlGrey launches APP with black screen

  26. 26

    How to show paused video instead of black screen when starting AVPlayer

  27. 27

    How to proceed after getting black screen when booting Ubuntu 20.04?

  28. 28

    Black Screen when Computer Boots

  29. 29

    Black screen when logging into desktop

HotTag

Archive