Search code examples
androidandroid-intentsplash-screenactivity-finish

How to make MainActivity ready before SplashActivity finish?


I'd like to make my splash screen more smooth. I maked splash screen after I read this link. However, when I finish SplashActivity and intent to MainActivity, I feel SplashActivity finish too early, I saw screen saver of my device before MainActivity show (I estimate the delay to be 0.5s). What I want is the MainActivity ready before the SplashActivity finish.

I tried the solutio that is set flags FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK in my Intent, but it's not make smooth.

Below is the code for transtion between 2 activities:

private void transitionActivity() {
        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }

And Manifest file:

<activity
            android:name=".SplashActivity"
            android:launchMode="singleTask"
            android:theme="@style/SplashScreenTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <activity
            android:name=".MainActivity"
            android:launchMode="singleTask"
            android:theme="@style/AppThemeNoActionBar" />

Solution

  • maybe the point is he want to give delay in splash screen

    if that so you can do that using Handler()

    int SplashDuration = 2000;
    new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Intent i = new Intent(splashScreen.this, MainActivity.class);
                        startActivity(i);
                        finish();
                    }
    
                }, SplashDuration);
    

    and if that still to fast or to slow for you, you can change the time at "SplashDuration"