Creating a Splash

0

I'm creating a splash but this splach is stopping my App, it works for a few seconds then automatically closes my App stops working and does not open anymore.

public class Splash extends AppCompatActivity {

    private final int SPLASH_DISPLAY_LENGTH = 3000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.hide();
        }

        new Handler().postDelayed(new Runnable(){
            @Override
            public void run(){
                Intent startActivityIntent = new Intent(Splash.this, AuthUIActivity.class);
                startActivity(startActivityIntent);
                Splash.this.finish();
            }
        }, SPLASH_DISPLAY_LENGTH);
    }
}
    
asked by anonymous 19.01.2018 / 17:26

1 answer

1

Splash Screens is not very good, as you slow down the time it takes to open the app, and users do not like slow apps.

However, if you need to do some processing (check credentials, check for updates, initialize database) before the main screen of your app is displayed, then displaying a splash is not a bad idea as long as it lasts exactly the time required to perform the necessary startup.

Implementation example:

Create an XML ( e.g. splash_background.xml ) in the res/drawable

<?xml version=”1.0" encoding=”utf-8"?>
<layer-list xmlns:android=”http://schemas.android.com/apk/res/android">

     <item android:drawable=”@color/colorPrimary” />

     <item>
         <bitmap
             android:gravity=”center”
             android:src=”@mipmap/ic_launcher” />
     </item>
</layer-list>

Create a theme for the Splash screen in the file styles.xml

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/splash_background</item>
</style>

No Manifest , set your SplashActivity as the main screen and set its theme to SplashTheme

<activity
    android:name=".SplashActivity"
    android:theme="@style/SplashTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

Now, in the code of your SplashActivity, just make the necessary initializations, and at the end of them, call the other Activity ( and. HomeActivity )

public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Obs.: Não é necessário chamar setContentView() pois o tema
        //SplashTheme dessa Activity já define uma imagem de fundo.

        // Códigos de inicialização do app

        startActivity(new Intent(this, HomeActivity.class));
        finish();
    }
}

Sources: Right Way to create Splash Screen on Android

    
22.01.2018 / 22:47