Creating a Splash Screen for Android

Pay It Square utilizes a token based authentication approach to indicate an authenticated request and map the request to the particular user authenticated.  This token is generated during the login process and can be stored on the device itself for future use.

In addition to the login, it is necessary to also load some satellite data for currency information, finally if the request is authenticated we attempt to load the Collect Pages for the user.

While this could all be done in an Async Task associated with the Login Screen, I felt it would be a better experience to use a Splash Screen, in which we would perform these operations and provide a friendly indication of progress to the user.  The following code shows the Async process taking place as part of the load.  The Splash Screen is really no different then a normal Activity in the app, except that there is less control on the part of the user as to when it finishes, its handle programmatically instead.

The processing should be done off the onCreate method through an async task.  You don’t want to do this on the main thread and risk hitting the 5s limit the Android places on applications before considering “Not Responding”.

The important thing here is that when the process is complete that you call finish on the activity as you start the next activity (see below):

   1: @Override

   2: protected void onPostExecute(Boolean bool) {

   3:     if (bool.booleanValue()) {

   4:         // show the login page

   5:         showLoginActivity();

   6:     }

   7:     else {

   8:         // show the main page

   9:         showMainActivity();

  10:     }


  12:     finish();       // kill the splash activity

  13: }

The reason you do this is to the user cannot reach the splash screen with the hardware back button.  This is a common tactic with Android apps so as to enforce control over the path the user takes, especially with respect to the user using the back button.

One of the things that I will point out that differs here from the previous version of the Pay It Square Android app (unreleased) is that instead of using SQLite, I used the Preferences engine to store the token rather then the database (see below):

   1: public class Preferences

   2: {

   3:     private final String AUTH_TOKEN_KEY = "authToken";

   4:     private final String AUTH_USERNAME = "username";


   6:     private SharedPreferences sharedPreferences;


   8:     public Preferences(Context context) {

   9:         sharedPreferences =

  10:             PreferenceManager.getDefaultSharedPreferences(context);

  11:     }


  13:     public String getAuthTokenKey() {

  14:         return sharedPreferences.getString(AUTH_TOKEN_KEY, "");

  15:     }


  17:     public void setAuthToken(String token) {

  18:         SharedPreferences.Editor editor = sharedPreferences.edit();

  19:         editor.putString(AUTH_TOKEN_KEY, token);

  20:         editor.commit();

  21:     }


  23:     public void clearAuthToken() {

  24:         setAuthToken("");

  25:     }


  27:     public String getStoredUsername() {

  28:         return sharedPreferences.getString(AUTH_USERNAME, "");

  29:     }


  31:     public void setStoredUsername(String username) {

  32:         SharedPreferences.Editor editor = sharedPreferences.edit();

  33:         editor.putString(AUTH_USERNAME, username);

  34:         editor.commit();

  35:     }


  37:     public boolean hasValidAuthToken() {

  38:         return getAuthTokenKey().compareTo("") != 0;

  39:     }

  40: }

This is a simple wrapper class that saves the AuthToken and Username to the Preference file associated with the app.  This is a very easy way to save and retrieve simple data rather then incurring the cost of setting up and maintaining a SQLite database on the device.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s