0

I have a splash screen and use a timer thread to display it for 2.5 seconds, that works fine but I get a blank page. If I remove the timer thread, my page displays perfectly.

  Thread splashTimer = new Thread(){
        public void run(){
            try{
                int splashTimer = 0;
                while (splashTimer < 2500){
                    sleep(200);
                    splashTimer = splashTimer + 200;
                    Intent navMainPage = new Intent("com.extempor.cheetah.CLEARSCREEN");
                    startActivity(navMainPage);

                }
            }
            catch(InterruptedException e){
                e.printStackTrace();
            }
            finally{
                finish();
            }


    }


};
splashTimer.run();

The page when it displays correctly shows a png image. I tried just a textview but nothing works while I have the timer thread. I'm using 2.3.3 and Eclipse 3.7

It seems that the timer thread is the standard way to show a splash screen, but why is no content displayed when I use it?

user913240
  • 313
  • 1
  • 3
  • 8

3 Answers3

1

First and foremost you should absolutely not make your users wait for any amount of time that is not absolutely necessary. If you need to do some kind of time consuming work at start up then show a splash screen during that and use a handler callback to hide it when the work is complete. Making your users wait for 2 or 3 seconds for no reason just so you can show them a pretty picture is silly and will turn people off from using your application IMO.

I achieved this effect using a fadeOut animation.

see this question for example animation code. If you use the code from that example you can use something like this to delay it for 2 seconds

    Runnable r = new Runnable(){
        public void run(){
            introLayout.startAnimation(fadeOutAnim);
            introLayout.setVisibility(View.GONE);

        }
    };
    Handler h = new Handler();
    h.postDelayed(r, 2000);

Again please really consider using a splash only if you have some real work to do, don't force your users to wait for 2 seconds with no good reason.

Community
  • 1
  • 1
FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
1

Never do anything related to UI in a non-UI thread, like a Timer for instance. Use an AsyncTask if you need to show a splash screen while doing some other jobs.

AsyncTask has 3 main methods:

OnPreExecute(), execute inside UI thread and fired before executing async thread, you can do anything inside here, like show your splash screen.

DoInBackground() this is executed in its own thread, outside UI thread. You can't update UI here, this is mainly to do any setup task your application need, while user reads your splash screen.

OnPostExecute() this is fired when DoInBackground() is done and runs in UI thread, you can dismiss your splash screen in here.

Take a reading here

ruhalde
  • 3,521
  • 3
  • 24
  • 28
0

I do suggest Tim's answer. However guess, you have come all the way correct till last line in your code snippet.

i.e when you called, "SplashTimer.run()" !

There is a difference between starting a thread using run() and start(), which you need to understand.

To be short & simple, run() : starts the thread synchronously. start() : starts the thread asynchronously.

For more discussions you may explore this link

Community
  • 1
  • 1
Chandan Adiga
  • 576
  • 4
  • 14