Splash Screens: Activity vs View

When designing the architecture for an Android application a common feature request is to have a splash screen while the application launches.

This feature and UI component can sometimes be utilized for branding or to present the user with a UI component while essential aspects of the application are loaded before fully launching.

Although there are several different implementations to accomplish the desired splash screen we will look at two different styles a splash screen Activity and a splash screen View.  Both of these splash screen implementations will include an asynctask which will run dummy operations to simulate a real splash screen.

First let us take a look at an implementation of splash screen as an activity

SplashActivity.java

package com.Tikal.activitysplash;

import android.app.Activity;

import android.content.Intent;

import android.os.AsyncTask;

import android.os.Bundle;

 

public class SplashActivity extends Activity {

                @Override

                protected void onCreate(Bundle savedInstanceState) {

                                super.onCreate(savedInstanceState);

                                setContentView(R.layout.activity_splash);

                                SplashSync sync = new SplashSync();

                                sync.execute(new String[] { "" });

                }

               

                private class SplashSync extends AsyncTask<String, String, String>{

                                @Override

                                protected String doInBackground(String... params) {

                                                //do some stuff

                                                for(int i=0; i < 20; i++){

                                                                try {

                                                                                Thread.sleep(20);

                                                                } catch (InterruptedException e) {

                                                                                e.printStackTrace();

                                                                }

                                                }

                                                return "";

                                }

                                @Override

                                protected void onPostExecute(String result) {

                                                super.onPostExecute(result);

                                                //all our syncing is done so lets start the main activity

                                                Intent MainActivity = new Intent(getApplicationContext(), MainActivity.class);

                                                SplashActivity.this.startActivity(MainActivity);

                                                SplashActivity.this.finish();

                                }

                }

}

 

activity_splash.xml

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity" >

 

    <ImageView

        android:id="@+id/imageView1"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:layout_alignParentRight="true"

        android:layout_alignParentTop="true"

        android:src="@android:drawable/ic_menu_compass" />

 

</RelativeLayout>

 

In this implementation once the asynctask is finished the MainActivity is launched and this activity is closed and removed from the stack.   Although this implementation is probably the most popular it can have some drawbacks.  First of all there is all the extra overhead; to initialize, sustain, and tear down the activity which would take valuable resources in memory and computations.

 

A lighter and more conservationist approach would be to add into our launch activity of our application the splash component as a view.  Although this will be lighter on resource usage, it is not as optimal when it comes to modulation since the splash logic and layouts will be somehow included in the normal logic.  A simple example of how to implement this type of splash screen is show below.

 

EasySplashActivity.java

 

package com.Tikal.viewsplash;

import java.util.Random;

import android.app.Activity;

import android.os.AsyncTask;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.animation.AlphaAnimation;

import android.widget.ImageView;

import android.widget.TextView;

 

import com.androiddev101.easysplash.R;

 

public class EasySplashActivity extends Activity {

 

                TextView mTextResult;

                ImageView mSplash;

                @Override

                public void onCreate(Bundle savedInstanceState) {

                                super.onCreate(savedInstanceState);

                                setContentView(R.layout.activity_easy_splash);

                                mTextResult = (TextView)findViewById(R.id.result);

                                mSplash = (ImageView)findViewById(R.id.splash_screen);

                                new EasySplash().execute();

                }

 

                class EasySplash extends AsyncTask<Void, Integer, Integer>{

                                @Override

                                protected Integer doInBackground(Void... params) {

                                                int madTest = 10000000;

                                                int result = 0;

                                                Random randomizer = new Random();

                                                int factorGnome = randomizer.nextInt(madTest);

                                                for(int i=0;i<madTest;i++){

                                                                if((i % factorGnome) == 0){

                                                                                Log.d("gnome","factored by Gnomes");

                                                                                result++;

                                                                }

                                                }

                                                return result;

                                }

 

                                @Override

                                protected void onPreExecute() {

                                                super.onPreExecute();

                                                mSplash.setVisibility(View.VISIBLE);

                                }

 

                                @Override

                                protected void onPostExecute(Integer result) {

                                                mTextResult.setText("the gnomes found " + result + " factors");

                                                mSplash.setVisibility(View.GONE);

                                                AlphaAnimation fadeout = new AlphaAnimation(1.0f, 0.0f);

                                                fadeout.setDuration(2000);

                                                mSplash.startAnimation(fadeout);

                                                super.onPostExecute(result);

                                }

                }

}

 

activity_easy_splash.xml

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent" >

 

    <TextView

        android:id="@+id/result"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"

        android:layout_centerVertical="true"

        android:text="@string/hello_world"

        tools:context=".EasySplashActivity" />

 

    <ImageView

        android:id="@+id/splash_screen"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:scaleType="centerCrop"

        android:visibility="gone"

        android:src="@android:drawable/ic_menu_compass" />

</RelativeLayout>

Thank you for your interest!

We will contact you as soon as possible.

Send us a message

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com