简体   繁体   中英

Android: Splash Screen Implementation Not Working

I am trying to implement a splash screen before the MainActivity but cannot get it working. I followed the instructions found here: https://www.bignerdranch.com/blog/splash-screens-the-right-way/ . Currently nothing appears but a black delayed screen. Please tell me where I went wrong. Thanks.

Splash.java

public class Splash extends AppCompatActivity {

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

        Intent intent = new Intent(this, WelcomeActivity.class);
        startActivity(intent);
        finish();
    }
}

background_splash.xml

 <layer-list xmlns:android="http://schemas.android.com/apk/res/android">

        <item
            android:drawable="@color/colorAccent"/>
        <item>
            <bitmap
                android:gravity="center"
                android:src="@drawable/splash"/>
        </item>

styles.xml

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

Manifest

<activity android:name=".Splash"
            android:theme="@style/SplashTheme">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

Simple, Uninstall & install the app again, you will get the splash screen. The problem is due to the instant run in Android Studio

To test, give a delay before starting the new activity:

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
    startActivity();
  }
}, 100);

Below super.onCreate(savedInstanceState); add setContentView(R.layout.layout_name.xml);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM