How to do a well-coded splash screen
android, android-asynctask, java, splash-screen
Solution
When an activity is launched, Android starts a Zygote, an empty activity which does nothing, and sets your activity theme on it, then launches it. Once your activity is ready for display, it swaps the displayed activity to yours. For more info about Zygote, you can read this article by Cyril Motier
So to answer your question, you can do this :
- Create a small theme which a custom window background displaying your splash info (you can use 9-patch to center unscaled content);
- In your manifest, use this splash theme for your activity;
- in your activity's onCreate() method, call setTheme(R.id.yourActivityTheme) (call it before setContentView() );
- enjoy...
That way, your "splash screen" (ie : the zygote with your splash theme) will be visible until your activity is ready to be displayed.
Problem
We all know there is lots of tutorials about how to do a splash screen on Android. But we also know those are pseudo-splashscreens. I searched for many and I always saw `Thread.sleep(x)`. That's not well coded, this is just to make the app beauty and looking like a professional app, that's not what I want! Another problem with those splash screens is that they don't solve my problem because they only show it after the activity start and show the content view. I have an app that does lots of things while initializing and when the app starts the user sees a black screen for a few seconds, enough time to annoy. So that's why I want to show a well-coded splash screen that removes that black screen that appears before the content view has been set. I tried something. I included the splash screen (a `RelativeLayout`) into the layout that is set in the `MainActivity`, but as far as I know Android only shows the content after everything has been loaded, so if I'm trying to show some view from the content view I have to wait until everything has finished. Still, I'll send my code, it can help somehow... ``` @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new SplashTask().execute(); } private class SplashTask extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { initializeViews(); mSplashScreen.setVisibility(View.VISIBLE); } @Override protected Void doInBackground(Void... params) { return null; } @Override protected void onPostExecute(Void params) { Standard.Initiate(MainActivity.this); verifyListStats(); loadListAdapters(); setOnClickListeners(); mSplashScreen.setVisibility(View.GONE); } } ``` I tried to load some resources in `doInBackground(...)` but because I do some operations in `onResume()` that need those resources I can't do it (or at least I think I can't). Any idea? I heard about a built-in mechanism similar to iOS launch images, perhaps that can be a way.