• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Black Screen lag on App start up. Splash not Displaying?

Valten1992

Newbie
I thought I had this fixed but c'est la vie.

My app has a splash page on startup, during this time it will connect to a server and download a file, once the download is done a 3 second timer will start then the main page of the app will load.

However, due to crappy connection in my workplace, the download can sometimes take a while, during this a black screen will show until the download is done, then the splash page and timer will start. What I want to know is why the splash page will not display over this black screen? Have I missed something?

Code:
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        
        // initialise progress icon
        linProgressBar = (ProgressBar) findViewById(R.id.progressBar1);
        linProgressBar.setVisibility(View.VISIBLE);
        
        try {
            this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    initializeApp();//connect, download, disconnect
                    uiHandler.post(new Runnable() {
                        public void run() {

                                timer = new Timer();// timer to display loading screen
                                timer.schedule(new TimerTask() {
                                    @Override
                                    public void run() { // end page and bring up main page
                                        startActivity(new Intent(
                                                SplashActivity.this,
                                                PrototypeActivity.class));
                                        SplashActivity.this.finish();
                                    }
                                }, delay);

                        }
                    });
                }
            });
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
        }

    }
 
I think maybe you confused yourself with the runOnUI() and post() commands and all those anonymous inner runnables defined in the method params of other methods.

Anyways, it appears you are running everything on the UI thread.

When you want to display the splash -- you can just do this directly from onCreate(), no need for runnabbles/special commands.

onCreate() is already on the UI Thread.

Then, run the server stuff on a background thread. For this you can use
Code:
Thread myBackgroundThread = new Thread (Runnable r)
myBackgroundThread.start();
(Or, extend the thread class.)


When the background thread has completed then you send a Message to the handler, and tell it to call startActivity or whatever you need.

(This code goes in your runnable for the background thread)
Code:
myHandler.obtainMessage(int whatMessage).sendToTarget();


Then you can define an int (say 99) to be the code for "the background thread is done"
(This can go in your activity)
Code:
private static final int LOAD_DONE= 99;

Then the call would look like this:
(This code goes in your runnable for the background thread)
Code:
myHandler.obtainMessage(LOAD_DONE).sendToTarget();

And your handler would look like this:
(This can go in your activity)
Code:
Handler myHandler= new Handler() 
{

 @Override
 public void handleMessage(Message msg) 
 {
    switch (msg.what)
    {
        case LOAD_DONE:
            handleLoadDone();
            break;
    }
  }
}

Then you have your own "handleLoadDone()" method in the activity like this:

Code:
protected void handleLoadDone()
{
    // do something here, start your next activity, 
    // finish an animation, whatever
}

Think of the handler as the mailman between the thread and Activity (the UI thread).

The background thread can notify the handler that it's time to change the UI, and the Handler can safely run methods on the main UI thread/Activity.


There are other ways to do this of course, but I find this method is what works for me and is the most flexible for expanding later.

However, some other things you could use: AsyncTask, Loaders, etc...


hth
 
Back
Top Bottom