Creation of a Splash

Asked

Viewed 48 times

0

I’m creating a splash but this splach is stopping my App, it works for a few seconds after automatically closes my App stops working and no longer opens.

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);
    }
}
  • Log in when the app closes.

  • could be more specific because I didn’t understand it very well

  • When you are running the application, in Android Studio shows the log of what is happening in the 'Run' tab, when the application stops working appears the error in this log.

1 answer

1


It is not very good to use Splash Screens, because it slows down the time it takes to open the app, and users don’t like slow apps.

However, if you need to do some processing (check credentials, search for updates, boot database) before your app’s main screen appears, then displaying a splash is not a bad idea, as long as it lasts exactly the time necessary to perform the necessary initializations.

Example of implementation:

Create an XML (e. g. splash_background.xml) in the briefcase 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>

In the Manifest, configure 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 (e. g. 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.