Is there a way to show splashscreen during onCreate?
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 h开发者_运维技巧ave 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);
}
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?
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.
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
精彩评论