开发者

android UI not showing up while processing other data

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);


  开发者_StackOverflow  // 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 before onCreate returns. See the article Painless Threading for various ways to avoid this problem.


Did you try doing "Start reading the XML and filling the arrays" in onResume() of your activity and then transit to next activity? And do reading from network in threads/async tasks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜