0

In my app I have a separate Activity for the splash screen. In that activity i am using an ExecutorService to run a heavy background task to load the startup data to a Application singleton class. Also i am doing a billing check in this class to check if the users have purchased any in_app_purchases.

So my question is, i want to use the android 12 splash screen API. So by using that API and what would be the best way to load the startup data so that i can totally remove the separate splash screen activity. Could I hold the startup screen till the data load. Where to implement the ExecutorService, is it on the main activity. Thanks

Yasiru Nayanajith
  • 1,647
  • 17
  • 20

1 Answers1

0

Ideally it's not a good practice to load large amounts of data on the Splash Screen, mainly due to the impact it has on the UX.

Though, after going through the Documentation on Migrating to Android 12 Splash Screen, I guess you can implement this solution to achieve that.

// 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 if the initial data is ready.
                return if (viewModel.isReady) {
                    // The content is ready; start drawing.
                    content.viewTreeObserver.removeOnPreDrawListener(this)
                    true
                } else {
                    // The content is not ready; suspend.
                    false
                }
            }
        }
    )
}

This solution basically delays the first frame of your app, hence allowing you to load the data that you want and allowing the user to move past the Splash Screen once you are done with it.

Hope this helps, let me know if you need more info on the same.

hsm59
  • 1,991
  • 19
  • 25
  • 1
    Can you explain the impact on the UX? To my understanding it is good practise to load large data upfront if there already is a splash screen present. The splash screen would be the same with or without loading while the user would not have to wait extra afterwards ( or before the splash screen) to use the app. – miva2 Aug 24 '21 at 07:33
  • 3
    It delays the use of the app, if there's a large amount of data to be fetched, you should do it asynchronously / in the background, so that it doesn't delay the user from using the app. If a particular feature depends on the data, check if the data has been loaded or not, and redirect accordingly. The Splash Screen is mainly to load / initialize your objects and load small amount of data / setup config, so that the experience is smooth once the user is past that screen. – hsm59 Aug 24 '21 at 07:39