Splash Screen (note: here and below - the loading screen) just takes your time, right? As an Android developer, when I see the loading screen, I immediately imagine how some bad developers add a three-second delay in the code.
I have to look at some image for a few seconds, and until then I can not use the application. And I have to do it every time after starting the application. I know which application I launched. I know what it does. Just let me use it!
What does Google recommend
You will be surprised to know that Google supporters are using Splash Screen. This is described right
here in the Material Design specification.
This was not always the case. Google was against Splash Screen, and even
called it an anti-pattern .
')
What do I see?Correct Splash Screen
I believe that Google does not contradict itself. Old advice and new recommendations are well combined. (However, it’s still not a good idea to use a loading screen which takes the user's time. Please do not do this)
However, Android applications take a certain amount of time to run, especially during a cold start. There is a delay that you cannot avoid. Instead of showing a blank screen, why not show the user something good? It is for this approach that Google advocates. It is not worth the time of the user, but do not show him the empty, unconfigured section of the application when he starts it for the first time.
If you look at the latest Google app updates, you'll see a similar way to use the loading screen. For example, take a look at the YouTube app:
The amount of time you spend watching Splash Screen exactly matches the amount of time it takes for the application to start. With a cold start, it means that the Splash Screen will be visible longer. And if the application is already cached, the screen saver will disappear almost immediately.
Splash Screen Implementation
Implementing Splash Screen in the right way is slightly different from what you can attach to yourself. The Splash Screen view that you see should be ready immediately, even before you can inflate (note: inflate) the layout file in your Splash Activity (note: Activity).
Therefore, we will not use the layout file. Instead, we will indicate the background of our Splash Screen in the background of the theme of its Activity. To do this, you first need to create an XML drawable in res / drawable.
Note: all the code below is available on GitHub .<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/gray"/> <item> <bitmap android:gravity="center" android:src="@mipmap/ic_launcher"/> </item> </layer-list>
Here I set the background color and image.
Next, you should set this drawable as the background for the theme of your Splash Screen Activity. Go to the styles.xml file and add a new theme for Splash Screen Activity:
<resources> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> </style> <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar"> <item name="android:windowBackground">@drawable/background_splash</item> </style> </resources>
In your new SplashTheme, set your XML drawable as a background. And install this theme in your Splash Screen Activity in your AndroidManifest.xml:
<activity android:name=".SplashActivity" android:theme="@style/SplashTheme"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
And finally, your SplashActivity class should redirect you to your main Activity:
public class SplashActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = new Intent(this, MainActivity.class); startActivity(intent); finish(); } }
Note that you do not customize the view for SplashActivity. The presentation is taken directly from the topic. When you define your Splash Screen Activity view through a theme, it is available immediately.
If you have a layout file for your Splash Activity, it will be shown only after your application is fully initialized, and that is very late. After all, we want Splash Screen to display only a small amount of time before the application is initialized.
Do it right
As soon as the steps above are completed, you will get the Splash Screen implemented in the correct way:
Armed with this knowledge, make your Splash Screen work correctly. Do not waste your time users, but give them something that they will be pleased to watch while they are waiting.