1

Maybe it is not the best approach, but my application use onCreate for loading/preparing quite many layouts into ViewAnimator, so after the application start, I have every screen of my app prepared for use. So changing different screens is smooth and fast.

The drawback of this method is, that it takes 5 seconds for the first layout to appear. To be exact - all screens of ViewAnimator are defined in XML layout and I supose they are inflated during onCreate.

My application must have a splashscreen, so my question is, if there is some way to use also these 5 seconds to show some image?

EDIT :

Thanks to your answers I came up with this simple solution:

Activity activity;
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_splash);
    activity = this;
    LinearLayout splash_layout = (LinearLayout) this.findViewById(R.id.splash_layout);

    // after 1 second of splash screen, start initializing everything
    splash_layout.postDelayed(new Runnable() 
    {   
        public void run() 
        {
            activity.setContentView(R.layout.main);

            // Here init whole layout and all class
            // During initialization, the splashscreen is still visible
        }
    }, 1000);
}
peter.bartos
  • 11,855
  • 3
  • 51
  • 62

3 Answers3

2

I don't believe any screen will be visible until after the onResume has exited and the main thread begins to service messages. Can you use 2 .xml layouts? One that loads the splash screen immediately on start up then kick off loading the rests of your screens with a layout inflator after the activity has begun?

Gregg Rivinius
  • 775
  • 1
  • 8
  • 13
  • You are absolutely correct; I deleted my answer in favor of this one. What the person who asked the question should do is to create a new Activity which displays the splash screen and then launch their activity which displays the primary layout. This way the splash screen will be displayed until the primary layout is displayed. – Rob S. Aug 16 '11 at 21:48
  • Yes, this looks like the only option also for me so far. First, it seemed dificult to implement this, but if I have only one main.xml on the top of everything, maybe it should work. As I see, there is also setContentView() with view as parameter. – peter.bartos Aug 16 '11 at 21:56
  • It works! I updated my question with a simple solution. Thank you. – peter.bartos Aug 22 '11 at 15:04
1

If I were you I would move long-running tasks out of the Activity.onCreate() method, since it may lead to ANR = Application Not Responding error, say to another thread.

pkk
  • 3,691
  • 2
  • 21
  • 29
0

To achieve this effect in my apps I just use a view with whatever image you want in it. And use the fade in and fade out animations.

Check out this question for one possible solution: Android change layout dynamically

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