Raunak Jain
Raunak Jain

Reputation: 1

How to change Android 12 Splash Screen duration?

I have this code where, when I launch the app, the animated icon animates for 800ms then the splash screen should get dismissed, but it stays for about a total of 1500ms before getting dismissed. How can I manually set the splash screen to dismiss as soon as the 800ms of animation ends.

This following is my splashscreen theme:

<style name="Theme.App.Starting" parent="Theme.SplashScreen">
    <item name="windowSplashScreenBackground">@color/color_accent</item>
    <item name="windowSplashScreenAnimatedIcon">@drawable/animation_test</item>
    <item name="windowSplashScreenAnimationDuration">800</item>
    <item name="postSplashScreenTheme">@style/AppTheme</item>
</style>

This is the onCreate() method of my first activity:

override fun onCreate(savedInstanceState: Bundle?) {

    installSplashScreen()

    super.onCreate(savedInstanceState)

    setContentView(R.layout.activity_main)
    ...
}

Also, is the delay because of memory issue or something else, as Im not sure if it is taking exact 1500ms everytime or is variable

Upvotes: 0

Views: 1831

Answers (2)

Sabya
Sabya

Reputation: 978

In may case i am using it check if the access token is there in the prefernce and it working fine in my case. It is blocking till result received from viewmodel

 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: 1

Mert
Mert

Reputation: 1005

Then we can try with a little trick use below code instead of only installSplashScreen():

        var splashScreenStays = true
        val delayTime = 800L

        installSplashScreen().setKeepOnScreenCondition { splashScreenStays }
        Handler(Looper.getMainLooper()).postDelayed({ splashScreenStays = false }, delayTime)

Edit: I've tested with physical android 12 device, and now tested with emulator with API 32, both works. Here is the gradle i have:

        compileSdk 33
        minSdk 21
        targetSdk 33

        implementation 'androidx.core:core-splashscreen:1.0.0'

And import should look like:

import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import android.os.Handler
import android.os.Looper

If all the code same as yours then something else in your other code must effecting that.

Upvotes: 1

Related Questions