I went through the documentation on the splash screen for Android 12 and also on migrating existing splash screen but it doesn’t mention clearly how to select the start screen based on the condition.
Specifically, I had added the check if the user is signed in like this previously:
class SplashScreenActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash_screen)
if(viewModel.isUserSignedIn()) {
navigateToMain()
} else {
navigateToAuth()
}
}
}
where the SplashScreenActivity
was marked as the default launch activity. When I keep it like this, Android studio gives a warning – The application should not provide its own launch screen
.
I modified it as per the guide and removed setContentView
from the activity and called installSplashScreen
.
class SplashScreenActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
installSplashScreen()
if(viewModel.isUserSignedIn()) {
navigateToMain()
} else {
navigateToAuth()
}
}
}
When I run on the emulator, it seems to be working as expected but the Android Studio continues to give the same warning.
Is this the right way to migrate to the Android 12 splash screen, if yes, why is the error on Android Studio still showing?
Also, do we need to keep the activity as the default launcher or is there a change required there?
2
Answers
You still need to call
setContentView
here.So the code would look like:
It’s because you’re creating an activity just for the SplashScreen. In the documentation, they state that the SplashScreen should be called in MainActivity here
Basically, you either need to set your splashScreen in the MainActivity or do this after migrating: