스플래시 화면 공식문서는 꼭 영어로 보기를 권한다. 한국어 버전은 이미지가 깨져서 보이지 않는다.
스플래시 화면은 이전에는 직접 만들어 사용했지만 Android12 이상 부터는 SplashScreen API가 생겼다. 따라서, 이전에 만들어둔 스플래시 화면을 SplashScreen API로 migrate해야 한다.
dependencies {
implementation "androidx.core:core-splashscreen:1.0.0"
}
SplashScreen의 배경은 Window이기 때문에 Window 관련 theme 값을 변경해야 한다.
windowSplashScreenBackground
windowSplashScreenAnimatedIcon
windowSplashScreenIconBackgroundColor
windowSplashScreenBrandingImage
windowSplashScreenBehavior
Activity.getSplashScreen()으로 스플래시 화면에 접근해 setOnExitAnimationListener를 통해서 스플래시 종료 애니메이션을 설정해줄 수 있다.
// Create a new event for the activity.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Set the layout for the content view.
setContentView(R.layout.main_activity)
// Set up an OnPreDrawListener to the root view.
val content: View = findViewById(android.R.id.content)
content.viewTreeObserver.addOnPreDrawListener(
object : ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean {
// Check whether the initial data is ready.
return if (viewModel.isReady) {
// The content is ready. Start drawing.
content.viewTreeObserver.removeOnPreDrawListener(this)
true
} else {
// The content isn't ready. Suspend.
false
}
}
}
)
}
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
// Handle the splash screen transition.
val splashScreen = installSplashScreen()
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
class RoutingActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
val splashScreen = installSplashScreen()
super.onCreate(savedInstanceState)
// Keep the splash screen visible for this Activity
splashScreen.setKeepOnScreenCondition { true }
startSomeNextActivity()
finish()
}