简体   繁体   中英

Android 12 splash screen conditional start screen

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?

You still need to call setContentView here. So the code would look like:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    installSplashScreen()
    setContentView(R.layout.activity_splash_screen)

    if(viewModel.isUserSignedIn()) {
        navigateToMain()
    } else {
        navigateToAuth()
    }
}

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:

After you've migrated to the new splash screen experience for Android 12 and higher, your custom splash screen Activity is still left over, so you'll need to choose what to do with it. You have the following options:

  • Keep the custom activity, but prevent it from displaying
  • Keep the custom activity for branding reasons
  • Remove the custom activity, and adapt your app as needed

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