My app takes a lot of time to load at the start, the screen is blank meanwhile. Is there a way to display an image when the app starts and later display the layouts once its done loading?
6 Answers
The approach differs a bit if you have multiple Activities within your app, but if you are only talking about a single Activity then the Activity itself won't show anything until the onResume() is called. Prior to that, the only thing that will be displayed is the background image of the Application/Activity. Setting a background image for your Activity can greatly increase the perceived startup speed, since they will display immediately. Romain Guy discusses background images a bit here: http://www.curious-creature.org/2009/03/04/speed-up-your-android-ui/.
If your app has a lot of startup crunching to do then you should do that work on a separate thread and let the onCreate(), onStart(), onResume() proceed quickly. Those methods should try to do as little as possible so the Activity appears to the user as soon as possible. If you want a loading or splash screen to be visible while it loads there different approaches. For the sake of starting simple, let's say you just want to have a loading screen with no animation, just an image. One way to do this would be to set the Activities background image to the loading screen image via XML. In the onCreate() start a thread with your loading processes, so it doesn't have to wait for it to finish in order to continue starting the Activity. In the XML layout, set the root ViewGroup for your layout to have a visibility of gone. This way your views won't appear and it will just show the background loading image. It will also allow these views to be inflated and setup.
Then, when the processes are finished, set the visibility of the ViewGroup to visible to have it appear to the user and if you want, you can change the background image too.
If you don't want to go the splash screen route, still make sure no long running methods are happening in the start up methods, get those onto another thread. You can show your Activity right away and just put up a loading animation if need be.
Obviously there are a ton of approaches and it all depends on what you really need. Hope that helps in someway.

- 21,220
- 37
- 123
- 171
-
thank you for the detailed reply. i am bad at handling threads and have no idea how to do it. the splash screen idea sounds good but i would need to learn how threads work. would you be aware of any tutorials that explain the same. thanks – i_raqz Dec 09 '10 at 23:42
-
Probably, the easiest route to go is using AsyncTask. If you look that up there are some tutorials and basic documentation about it. It allows you to easily say "Go do A, and when you are done do B". The other methods are looking into Threads, Runnables and Handlers and also Executors. AsyncTask is a easy way to start though. – cottonBallPaws Dec 09 '10 at 23:49
I want to update this question to this year, because I had the same problem and I found it in google.
In values directory, if you do not have it, create a file of styles like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppBaseTheme" parent="android:Theme.Holo.NoActionBar.Fullscreen">
<item name="android:windowBackground">@color/your_color</item>
</style>
</resources>
Now go to AndroidManifest.xml, and add the style to your activity launcher:
<activity
android:name=".Main"
android:theme="@style/AppBaseTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

- 647
- 10
- 17
I would ask myself - WHY does it take a long time to load? Did you profile it? Can you improve that? You should focus on that.
Barring that, yes, you could add a splash screen (something that I recommend people to NOT do) - you could either switch between two layouts using a ViewFlipper or FrameLayout, or you could have two separate activities.

- 76,846
- 14
- 164
- 167
-
Why do you not suggest a splash screen? They are pretty ubiquitous and almost everyone knows what they are when they see one. One main tenant of UI design (as far as I was trained) is to not surprise the user. A splash screen is something they are used to seeing. Just curious why you think splash screens are not the way to go - I may just change the way I think :) – SRM Dec 09 '10 at 02:20
-
I find splash screens ok, but don't like if you cannot tap them to proceed but are forced to wait until whatever delay the developer put in. – Mathias Conradt Dec 09 '10 at 02:36
-
well..the app reads a RSS feed and displays the information in form of lists. so the entire RSS feed has to be parsed else it there is no point starting the activity...there are over 40 items in the feed... – i_raqz Dec 09 '10 at 23:45
-
Look at Google Reader - it retains the previous state and lets you work with it while it updates in the background. Personally, I'd prefer that. But again, if you really want to make it synchronous, have a splash activity, read your RSS feed then and populate your database, then simply go to the next activity. – EboMike Dec 10 '10 at 00:11
Theres a couple of ways to do this, but the easiest, and most straightforward would be to launch your long loading activity from you splash screen activity.
Another technique is to offload the initialization and loading to a separate thread.

- 1,377
- 1
- 10
- 17
-
this seems to be the most simple..but how do i know when the main activity;s job is over and the splash displaying activity can start that... – i_raqz Dec 09 '10 at 23:44
You could also do all the time consuming stuff in a background thread and use Viewgroup.addView once your layout is inflated and all resources are loaded.
- Put some minimum layout in your activity
- Create a thread in onStart to load your resources and layouts using LayoutInflator
- Once you are done post a Runnable to the main thread using a Handler to exchange the layout of the activity using addView

- 14,959
- 6
- 52
- 69
You need splash screnn. You can look at my answer thats explains splash screen.

- 1
- 1

- 1,720
- 1
- 15
- 23