0

I'm having trouble putting this problem into searchable terms. I'm working on an Android application, and specifically the splash screen for my app. The app needs to fetch data from an external web service (a blocking function call), while it does this the user gets a nice title, image and progress bar. When the data arrives the user is redirected to the main menu. Its a simple screen, everything being defined in the xml layout file, my problem is that I just get a black screen for a few seconds and then the main menu. If I press back I get the splash screen with the progress bar spinning away happily.

Here is what I have so far:

public class SplashActivity extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
    }

    @Override
    public void onStart(){
        super.onStart();
        DatabaseManager db = new DatabaseManager(this.getBaseContext());
        db.fetchExternCatalog(); //doesnt return until data arrives
        Intent intent = new Intent().setClass(this, MainMenuActivity.class);
        startActivity(intent);
    }
}

It seems the screen isnt actually drawn until the activity is running (after onCreate(), onStart(), etc). I thought onStart() would be the perfect place to put this, but apparently not.

So how do I draw everything on the screen and make my blocking function call after so the user actually sees the splash screen while the data is downloaded?

macgregor
  • 150
  • 1
  • 1
  • 10
  • Looks like I found the answer to my own question, should have looked at the related list on the side before posting (still sort of new here ha):http://stackoverflow.com/questions/1979524/android-splashscreen, did the trick – macgregor Apr 09 '12 at 18:40

2 Answers2

0

you need to use the ProgressDialog class to build the dialog, and then run the blocking method inside a thread.

I'll post an example in a minute (gotta get near a PC :p)

    private void showSplash(){

    progressDialog = ProgressDialog.show(this, "Hello! ima title", "Im the message you see.");
    progressDialog.show();

    Thread t = new Thread(new Runnable(){                   
        public void run(){
            // Put your blocking method here.
            // You may need to build in a "hey, im done downloading" variable to get it to close down right
    progressDialog.dismiss();
    }
}); 
t.start();
}
z3nful
  • 81
  • 1
  • 8
  • Can you add images and what not to ProgressDialog's? I assume you could if you make your own subclass, but that seems like more work than necessary for a splash screen this simple. – macgregor Apr 09 '12 at 18:44
  • You should be able to add an image through setContentView() (never tried it myself) i believe you just need to remove the message part so it doesnt conflict with the content. – z3nful Apr 09 '12 at 18:48
  • I might try that later, could save some resource overhead creating a separate activity would cause – macgregor Apr 09 '12 at 18:53
0

You're going to be locking up the UI thread which is why i believe you are seeing a black screen. Use an AsyncTask or create your own thread pool for DB operations.

As far as hitting the back button and seeing your old activity, You need to tell android not to store the activity in it's stack. This should help:

http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html

Paul Nikonowicz
  • 3,883
  • 21
  • 39
  • That's what I ended up doing, was about to get on the back button part. Ill take a look at that link, thanks. – macgregor Apr 09 '12 at 18:41