Pages

Wednesday, October 12, 2011

Activity Constructor vs. onCreate

This isn't explicitly stated in many tutorials, but something I've learned from experience.  The onCreate method in Activities is called when the Activity starts, and books show all the set-up work being done in that method.  But what about the Activity constructor?  Can you use it for any initialisation work?

No.  In general you should leave it alone, as much of the Android API will not work if called from the Activity constructor.  For instance, trying to inflate a layout will raise an exception.  So restrict your initialisation to onCreate.

No comments:

Post a Comment