user141146
user141146

Reputation: 3315

Android Splash screen for "Application" class

I have an Android app where I've extended the base Application class in order to set up some global variables.

public class MyApplication extends Application {

 private ArrayList<ModelClass> master_list; // global variable 1
 private DataBaseHelper db_helper; // global variable 2

 @Override
 public void onCreate() {
  super.onCreate();
  //do database work that will take about 5 seconds
 }
}

I'd like to show a splash screen to the user while the Application class is working (i.e. before my Main Activity is created). Is there a way to do this?

Upvotes: 5

Views: 4539

Answers (4)

PSchuette
PSchuette

Reputation: 4473

I realize this is rather far after this was asked but I hope someone finds this useful and I will provide code if needed. My approach was to create a public boolean in the application class called "finishedLoading". I launch the application into my splash screen activity and I have that check every 250 milliseconds as to whether or not finishedLoading = true and when it does, I launch the new activity. another nice part about that is I have a little spinner progress bar so the user sees somethings happening and animation all around.

Upvotes: 0

Thomas Vervest
Thomas Vervest

Reputation: 2141

The way I do this in my apps is by having your main activity extend ActivityGroup.

public class App extends ActivityGroup {
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.splashscreen);
        new Thread(new Runnable() {
            public void run() {
                // do your loading here
                final LocalActivityManager lam = getLocalActivityManager();
                runOnUiThread(new Runnable() {
                    public void run() {
                        Intent mainIntent = new Intent(MainActivity.class, App.this);
                        Window w = lam.startActivity("main", mainIntent);
                        setContentView(w.getDecorView());
                    }
                }
            }
        }, "App loading thread").start();
    }
}

This way, when the app resumes you immediately get the app, not the splash screen, and you only load your data once (when the app starts).

I actually use this to make sure the user is logged in when the app starts, and if the username/password combination is not right I don't start the main app, but a login screen :)

Upvotes: 3

MatteKarla
MatteKarla

Reputation: 2737

You could make the SplashActivity your start activity.

When MyApplication has completed it's work you could start your main activity and dispose the splash screen.

But don't do the heavy database work in onCreate, create another function and do it there, otherwise your splash activity won't be shown.

public class SplashActivity extends Activity
    @override
    protected void onResume() {
    //Create a thread
    new Thread(new Runnable() {
            public void run() {
                //Do heavy work in background
                ((MyApplication)getApplication()).loadFromDb();
                startActivity(new Intent(SplashActivity.this, MainActivity.class));
                finish(); //End this activity
            }
        }).start();
    }
}

Upvotes: 5

lheezy
lheezy

Reputation: 542

First off, I think there are many approaches to having a splash screen. Intuitively, I would not have a special class/activity just to load specific information, because to keep that information, you will have to keep that activity alive and well somewhere- taking up resources.

I would have a splash screen just be a layout, that is initially loaded on with the main activity's onCreate(), and then once the DB information is loaded, change your layout to the main activities layout.

something like:

public void onCreate() {
    super.onCreate();
    setContentView(R.layout.splashscreen);
    //do database work that will take about 5 seconds
    setContentView(R.layout.mainscreen)
}

Upvotes: 0

Related Questions