简体   繁体   中英

Android 12 Splash Screen API - Increasing SplashScreen Duration

I am learning Android's new SplashScreen API introduced with Android 12. I have so far gotten it to work on my Emulator and Google Pixel 4A, but I want to increase its duration. In my Splash Screen I do not want a fancy animation, I just want a static drawable.

I know, I know (sigh) some of you might be thinking, that I should not increase the duration and I know there are several good arguments in favor of not doing so. However, for me the duration of a splash screen with a non animated drawable is so brief (less than a second), I think it raises an accessibility concern, especially so since it cannot be disabled (ironically). Simply, the organization behind the product or its brand/product identity cannot be properly absorbed or recognized by a new user at that size and in that time, rendering the new splash screen redundant.

I see the property windowSplashScreenAnimationDuration in the theme for the splash screen (shown below), but this has no effect on the duration presumably because I am not animating.

 <style name="Theme.App.starting" parent="Theme.SplashScreen">
        <!--Set the splash screen background, animated icon, and animation duration.-->
        <item name="windowSplashScreenBackground">@color/gold</item>
    
        <!-- Use windowSplashScreenAnimatedIcon to add either a drawable or an
             animated drawable. One of these is required-->
        <item name="windowSplashScreenAnimatedIcon">@drawable/accessibility_today</item>
        <item name="windowSplashScreenAnimationDuration">300</item> <!--# Required for-->
                                                                    <!--# animated icons-->
        <!--Set the theme of the activity that directly follows your splash screen-->
        <item name="postSplashScreenTheme">@style/Theme.MyActivity</item>
    
        <item name="android:windowSplashScreenBrandingImage">@drawable/wculogo</item>
    
    </style>

Is there a straightforward way to extend the duration of a non animated splash screen?

in Kotlin:

var keepSplashOnScreen = true
val delay = 2000L

installSplashScreen().setKeepOnScreenCondition { keepSplashOnScreen }
Handler(Looper.getMainLooper()).postDelayed({ keepSplashOnScreen = false }, delay)

you can put this into onCreate fun before super.onCreate calling (in activity with LAUNCHER intent filter in Manifest)

As I was writing this question and almost ready to post it, I stumbled on the method setKeepOnScreenCondition (below) that belongs to the splashScreen that we must install on the onCreate of our main activity. I thought it seemed wasteful not to post this, given there are no other posts on this topic and no such similar answers to other related questions (as of Jan 2022).

SplashScreen splashScreen = SplashScreen.installSplashScreen(this);
plashScreen.setKeepOnScreenCondition(....);

Upon inspecting it I found this method receives an instance of the splashScreen.KeepOnScreenCondition() interface for which the implementation must supply the following method signature implementation:

 public boolean shouldKeepOnScreen() 

It seems this method will be called by the splash screen and retain the splash screen visibly until it returns false. This is where the light bulb moment I so love about programming occurred.

What if I use a boolean initialized as true, and set it to false after a delay? That hunch turned out to work. Here is my solution. It seems to work and I thought it would be useful to others. Presumably instead of using a Handler for a delay, one could also use this to set the boolean after some process had completed.

package com.example.mystuff.myactivity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.splashscreen.SplashScreen;
import android.os.Bundle;
import android.os.Handler;

public class MainActivity extends AppCompatActivity {
    
    private boolean keep = true;
    private final int DELAY = 1250;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Handle the splash screen transition.
        SplashScreen splashScreen = SplashScreen.installSplashScreen(this);

        //Keep returning false to Should Keep On Screen until ready to begin.
        splashScreen.setKeepOnScreenCondition(new SplashScreen.KeepOnScreenCondition() {
            @Override
            public boolean shouldKeepOnScreen() {
                return keep;
            }
        });
        Handler handler = new Handler();
        handler.postDelayed(runner, DELAY);
    }

    /**Will cause a second process to run on the main thread**/
    private final Runnable runner = new Runnable() {
        @Override
        public void run() {
            keep = false;
        }
    };
    
}

If you have comments or feedback (besides telling me I should not increase the duration of the splash screen), or a better way please do comment or respond with additional answers.

One proxy way could be to use runBlocking { delay(1200) } in onCreate method, to keep on main thread for some specific time.

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