views:

763

answers:

1

I've read that it's important to call setContentView() early in an activity since it builds the view objects that may be manipulated by subsequent code in onCreate().

In terms of lifecycle, does the view get drawn to screen as soon as setContentView() is called, or does it allow the onCreate() function to build/populate the information in the view objects, and wait to actually draw it after onCreate() completes?

Thanks!

+2  A: 

does the view get drawn to screen as soon as setContentView() is called

No.

or does it allow the onCreate() function to build/populate the information in the view objects, and wait to actually draw it after onCreate() completes?

Yes. The View objects are created immediately as part of setContentView(). However, all drawing operations (from onCreate() or anywhere else) really result in messages being put on a message queue that the main application thread works through.

CommonsWare
Thank you!Can you point me toward a good resource to explain more in depth about the message queue that the main application thread processes? This is a new concept for me, as I'm just beginning Android programming.
stormin986
There isn't much written about it directly. The general rule of thumb is: if it modifies the screen, the actual screen change does not take effect when you call the method (e.g., `setText()` on a `TextView`). Rather, your method call creates an object that represents the request, which goes on the message queue. That queue is processed when the main application thread is not tied up running your code (e.g., `onCreate()`). This is why you are told to keep long-running stuff off the main application thread.
CommonsWare
This is probably obvious to most, but as a beginner I found this useful as well (from the documentation of the Handler object) since I wasn't aware of such a message queue:"When a process is created for your application, its main thread is dedicated to running a message queue that takes care of managing the top-level application objects (activities, broadcast receivers, etc) and any windows they create."
stormin986