A splash screen is just like any other activity, but it can handle all of your startup-needs in the background. Example:
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.package"
android:versionCode="1"
android:versionName="1.0" >
<application
android:allowBackup="false"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Splash"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Now our splash-screen will be called as the first activity.
Here is an example splashscreen that also handles some critical app elements:
public class Splash extends Activity{
public final int SPLASH_DISPLAY_LENGTH = 3000;
private void checkPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WAKE_LOCK) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(this,Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED) {//Can add more as per requirement
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WAKE_LOCK,
Manifest.permission.INTERNET,
Manifest.permission.ACCESS_NETWORK_STATE},
123);
}
}
@Override
protected void onCreate(Bundle sis){
super.onCreate(sis);
//set the content view. The XML file can contain nothing but an image, such as a logo or the app icon
setContentView(R.layout.splash);
//we want to display the splash screen for a few seconds before it automatically
//disappears and loads the game. So we create a thread:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//request permissions. NOTE: Copying this and the manifest will cause the app to crash as the permissions requested aren't defined in the manifest.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ) {
checkPermission();
}
String lang = [load or determine the system language and set to default if it isn't available.]
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config = new Configuration ();
config.locale = locale;
Splash.this.getResources().updateConfiguration(config,
Splash.this.getResources().getDisplayMetrics()) ;
//after three seconds, it will execute all of this code.
//as such, we then want to redirect to the master-activity
Intent mainIntent = new Intent(Splash.this, MainActivity.class);
Splash.this.startActivity(mainIntent);
//then we finish this class. Dispose of it as it is longer needed
Splash.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
public void onPause(){
super.onPause();
finish();
}
}