Reputation: 2987
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?
Upvotes: 13
Views: 20372
Reputation: 2987
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);
splashScreen.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 initialised 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) {
// Handle the splash screen transition.
SplashScreen splashScreen = SplashScreen.installSplashScreen(this);
super.onCreate(savedInstanceState);
//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 are into Java Lambdas an even nicer and more compact solution is as follows:
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) {
// Handle the splash screen transition.
SplashScreen splashScreen = SplashScreen.installSplashScreen(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Keep returning false to Should Keep On Screen until ready to begin.
splashScreen.setKeepOnScreenCondition(() -> keep);
Handler handler = new Handler();
handler.postDelayed(() -> keep = false, DELAY);;
}
}
Update Jan 2025: I have since been learning Kotlin and have developed the following solution for Kotlin. The delay is now brought about by Kotlin's co-routines feature.
private var keep : Boolean = true
private val DELAY = 1250L//Miliseconss
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
installSplashScreen().setKeepOnScreenCondition { keep}
lifecycleScope.launch{
delay(DELAY)
keep = false
}
enableEdgeToEdge()
setContentView(R.layout.activity_main)
//Rest of on create here
}
For this to work, the following imports will be needed for Kotlin:
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
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.
Upvotes: 20
Reputation: 1
You might also need this:
super.onCreate(savedInstanceState);
//this is made not to have a white screen after the main is killed (onDestroy is fired, which can be recreated with help "Do not keep activities") //think about less than '50' or even '0' to make the transition not noticeable at all if you like!
if (savedInstanceState != null){
DELAY = 50;
}
Upvotes: 0
Reputation: 9
Put below code in onCreate before super.onCreate(savedInstanceState)
and change delay time according to you
installSplashScreen().setKeepOnScreenCondition {
runBlocking {
delay(700)
false
}
}
Upvotes: 0
Reputation: 21
The Code:
var keepSplashAlive = true
runBlocking {
delay(2000)
keepSplashAlive = false
}
installSplashScreen().setKeepOnScreenCondition {
keepSplashAlive
}
Upvotes: 1
Reputation: 978
For me its working this way
override fun onCreate(savedInstanceState: Bundle?) {
val splashScreen = installSplashScreen()
super.onCreate(savedInstanceState)
var uiState: MainActivityUiState by mutableStateOf(MainActivityUiState.Loading)
// Update the uiState
lifecycleScope.launch {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.uiState
.onEach { uiState = it }
.collect()
}
}
// Keep the splash screen on-screen until the UI state is loaded. This condition is
// evaluated each time the app needs to be redrawn so it should be fast to avoid blocking
// the UI.
splashScreen.setKeepOnScreenCondition {
when (uiState) {
MainActivityUiState.Loading -> true
is MainActivityUiState.Success -> false
}
}
setContent {
if(uiState is MainActivityUiState.Success){
MainScreen((uiState as MainActivityUiState.Success).route)
}
}
@HiltViewModel
class MainViewModel @Inject constructor() : ViewModel() {
@Inject
lateinit var preferenceDataStoreHelper: MyPreferenceHelper
val uiState: StateFlow<MainActivityUiState> = flow<MainActivityUiState>{
val accessToken= preferenceDataStoreHelper.getPreference(ACCESS_TOKEN,"").first()
if(accessToken.isEmpty()){
emit(MainActivityUiState.Success(loginRoute))
}else{
emit(MainActivityUiState.Success(homeScreenRoute))
}
}.stateIn(
scope = viewModelScope,
initialValue = MainActivityUiState.Loading,
started = SharingStarted.WhileSubscribed(5_000),
)
}
sealed interface MainActivityUiState {
data object Loading : MainActivityUiState
data class Success(val route: String) : MainActivityUiState
}
Upvotes: 0
Reputation: 21
Kotlin, Compose:
To use postDelayed() or runBlocking { } is not the best solution. I prefer calculate setKeepOnScreenCondition using asynchronous calls. I.e., imagine that you need to show splash icon while you're collecting value from DataStore (to show or not onboarding screens), or pull some data from API/network. In view model:
private val _isLoading = MutableStateFlow(true)
val isLoading get() = _isLoading.asStateFlow()
private val _startDestination = MutableStateFlow<String?>(null)
val startDestination get() = _startDestination.asStateFlow()
init {
viewModelScope.launch {
delay(2000) // long operation
// use .collect if you're interested in collecting all emitted values
onboardingRepository.onboardingState().collect { isFinished ->
_startDestination.value = if (isFinished) {
_isLoading.value = false
Screen.Authentication.route
} else {
_isLoading.value = false
Screen.Onboarding.route
}
}
// or use .first() terminal operator that returns the first element emitted by the flow and then cancels flow's collection
val isFinished = onboardingRepository.onboardingState().first()
_startDestination.value = if (isFinished) {
Screen.Authentication.route
} else {
Screen.Onboarding.route
}
_isLoading.value = false
}
}
In MainActivity create a function, collect flow values and use them for onScreenConditions:
private val splashViewModel by viewModels<SplashViewModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setupSplashScreen()
setContent {
DiaryAppTheme {
val navController = rememberNavController()
val startDestination by splashViewModel.startDestination.collectAsStateWithLifecycle()
startDestination?.let {
SetupNavGraph(
navController = navController,
startDestination = it
)
}
}
}
}
private fun setupSplashScreen() {
var keepSplashScreenOn = true
lifecycleScope.launch {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
splashViewModel.isLoading.collect {
keepSplashScreenOn = it
}
}
}
installSplashScreen().setKeepOnScreenCondition {
keepSplashScreenOn
}
}
Upvotes: 2
Reputation: 91
One proxy way could be to use
runBlocking { delay(1200) }
in onCreate method, to keep on main thread for some specific time.
Upvotes: 0
Reputation: 279
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)
Upvotes: 12