I’m trying to read in 17 images from the Internet while displaying a splash screen. However with the following code, the splash screen does not show up at all during the processing. It goes black, finishes and transits to the next activity. I thought calling setContentView before the processing code is good enough but apparently not.
What am I missing?
Thanks for your help.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
// read the bitmaps
// Open a new URL and get the InputStream to load data from it
// Start reading the XML and filling the arrays
for (int i=0; i<16; i++) {
try {
URL aURL = new URL (districtImage[i]);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
// Buffered is always good for performance
BufferedInputStream bis = new BufferedInputStream(is);
districtBitmap[i] = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
}
catch (IOException e){
Log.e ("DEBUGTAG","Cannot load remote image", e);
}
}
... some more code to transit to next activity...
}
When you do networking operations inside
onCreate, you are blocking the UI thread until they complete. Nothing will show up on the screen beforeonCreatereturns. See the article Painless Threading for various ways to avoid this problem.