SteveEdson
SteveEdson

Reputation: 2495

Android onCreate and AsyncTask delay

My app needs to load some JSON before it can show anything on the UI, the JSON determines how the app should be themed.

Although I don't usually like them, a splash screen / loading screen is required so that the user doesn't have to see each element of the interface change colour etc. My problem is however, that when I load my app, I can see a default style for a few seconds before my progress dialog launches. I can see the default dark gradient holo background, with an actionbar containing my app title. If I hide the action bar in the XML, I cannot add it back in later, and get nullpointerexceptions when acting on it.

I have tried to launch the splash screen at the very beginning of the onCreate() before I even set up my AsyncTask, and also tried launching it in the onPreExecute().

I want to be able to ideally have a way of showing a loading dialog immediately when the app launches, or if there does have to be a delay, I just want the user to see a black background.

Is there a totally different way of doing this that I haven't mentioned?

Thanks.

Upvotes: 0

Views: 589

Answers (2)

Nick Campion
Nick Campion

Reputation: 10479

I'd actually consider using fragments here. You could have a fragment for your splash screen and load that when your activity first starts. Then, once your json loading / parsing is complete, you could load your second 'screen' as a second fragment using a FragmentTransaction and have it replace your loading screen. This would also let you do some nice transition animations.

This could be similarly done with the comment from njzk2, with two separate Activities. Essentially, you would do your parsing AsyncTask during the 'loading activity' and store the result as either a serializable object that could be passed to your other activity using an Intent extra or you could store the json into the SharedPreferences dictionary and reference that in your second activity.

You might also find that this is something you don't have to do every start up. If so, you should save either the serializable to disk so you can skip parsing or to the shared preferences so you can reference it there if it exists.

Upvotes: 1

Korniltsev Anatoly
Korniltsev Anatoly

Reputation: 3686

What you see is an empty activity themed according to your theme. So there is nothing you can do - it will be shown if the bootstraping activity takes some time.

What you can do is set special theme on your activity with splash-image set as windowBackground (window decor-view's background), and remove the background later on if you don't need it.

<style name="SplashScreenActivityTheme" parent="android:Theme.Light.NoTitleBar">
        <item name="android:windowBackground">@drawable/splash_image</item>
</style>

More about it you can read here in fresh post of Cyril Mottier

One more notice: android usually does not use splash screens. You can use some progress indicator such ProgressBar or something else if your designer is not against of it

Upvotes: 0

Related Questions