简体   繁体   中英

How to display the splash screen when the app start

How to display a splash screen when the app starts, I try to add a splash screen to the app but it is not working for me. Here is my code

SplashActivity

public class SplashActivity extends AppCompatActivity {

int SPLASH_TIME = 3000; //This is 3 seconds
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splach);

    //Code to start timer and take action after the timer ends
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            //Do any action here. Now we are moving to next page
            Intent mySuperIntent = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(mySuperIntent);

            //This 'finish()' is for exiting the app when back button pressed from Home page which is ActivityHome
            finish();
        }
    }, SPLASH_TIME);
}
}

Manifest

    <application
    android:allowBackup="true"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:roundIcon="@drawable/icon"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".SplashActivity" />
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

Your splash must be the launcher activity:

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

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

You should put this part in the tag of start activity.

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

For example:

<activity android:name=".StartActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

Hope it will help you a little.

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