When my quiz app starts for the first time it needs to add questions to a database and a few other things and this takes a few seconds. I want to display a indeterminate progressbar whilst this occurs. Once the questions are added, the class calls finish() and moves on to the main screen. The problem is that the layout isn’t drawn unless finish isn’t called. I assume it doesn’t draw the XML layout until after the oncreate and onstart are called. So how do I get around this?
Share
The reason, the UI isn’t shown while you’re filling your DB is probably that you’re doing that in the UI-Thread – the main thread of your application. As the thread is busy filling your DB, there is no time to show the UI, animate the progress indicator or react to user actions.
Thus you should always run time-consuming tasks in a separate thread. See this excellent entry in the android devloper’s blog about painless threading for more information and to do it right: