Android onCreate
Im currently trying to write an application in my own time in between school and work, and i have a question about onCre开发者_开发问答ate.
Does each class in the application need an onCreate or is it only the main class that needs one, like the first class that is called when launching the app.
Or is it only required on classes that have an XML that is going to be used with it, or a class displaying something.
Android has four types of application components:
- Activities
- Services
- Content Providers
- Broadcast Receivers
Each of your activities will have an onCreate( ). How many activities you want depends totally on your app and your choice. Services also have an onCreate( ).
Only the class extends Activity or Service can override onCreate, also you may not override it if you like.
Each Activity
or other component should have its own onCreate since that's where you are initializing your components when you starting it. You could think of it as a constructor for your them, you are free to implement your own initialization (or do nothing if you wish)
Every Activity uses onCreate() method. The method is used every time an activity is started.
You can learn more about the activity life cycle here
http://developer.android.com/training/basics/activity-lifecycle/starting.html
精彩评论