4

As my app (game) is getting larger and larger, I've started to encounter a problem.

I have a menu activity with a 'start game' button - when the user presses this button, it starts the main game activity - now in this activity, I'm creating bitmaps etc in the constructor but there are so many that now when the activity starts, there is a slight delay - about 2 seconds - before the game actually starts.

I'm clearly doing something wrong - please could someone advise how to get around this so the delay (which clearly, has to happen) - isn't noticed by the user.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Zippy
  • 3,826
  • 5
  • 43
  • 96

3 Answers3

3

Load the ones you need immediately right away. Load the rest on a background thread (probably an AsyncTask). If you need one before it may be loaded, either pause or put up a loading screen as needed.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Thank you all for your comments / answers - I am using surfaceView and am using a separate thread for that - are you suggesting I need to create yet another thread for loading of resources? Many thanks! – Zippy Jan 24 '13 at 21:47
1

You could start loading the bitmaps in the background of your menu activity, or even when your app is created using a background thread or AsyncTask. You'll still need a loading screen of some sort in case the user navigates to the main game activity before you're done loading all of the bitmaps though.

kabuko
  • 36,028
  • 10
  • 80
  • 93
  • Thank you - how would I do that? I mean how would I load stuff in one activity and still be able to use it in another activity - thanks! – Zippy Jan 24 '13 at 21:48
  • You can reference it from a singleton or a class exposed from a field in a custom application class. See this other question for arguments for each option: http://stackoverflow.com/questions/3826905/singletons-vs-application-context-in-android – kabuko Jan 24 '13 at 21:51
0

You can also kick off an IntentService to load the bitmap. When the bitmap is loaded the IntentService can send a broadcast using LocalBroadcastManager. Then each component that cares about a result can register a BroadcastReceiver with LocalBroadcastManager.

Matt Accola
  • 4,090
  • 4
  • 28
  • 37