views:

328

answers:

2

I understand that Android Activities have specific lifecycles and that onCreate should be overridden and used for initialization, but what exactly happens in the constructor? Are there any cases when you could/should override the Activity constructor as well, or should you never touch it?

I'm assuming that the constructor should never be used because references to Activities aren't cleaned up entirely (thus hampering the garbage collector) and that onDestroy is there for that purpose. Is this correct?

+1  A: 

You need to override the Constructor when your activity will have custom params or you want to track calls from classes that inherited from.

Pentium10
Can you elaborate on this more? What you describe sounds interesting, but it is a little vague. Thanks!
mxrider
Suppose you need to create a custom Activity class that takes 2 or more params. You just need to use the Constructor, you can't do that via the onCreate and extras. Does it help?
Pentium10
But you don't explicitly create Activities, you create an Intent...
mxrider
It might that I need a private one. Suppose I want to create a custom component for example a customized contact picker. In order to have `startActivityForResult` I must include a private constructor in my custom component, even if that activity will never be launched and has no visibile elements, I just use the for result stuff of it.
Pentium10
+2  A: 

I can't think of any good reason to do anything in the constructor. You never construct an activity directly, so you can't use it to pass in parameters. Generally, just do things in onCreate.

Mayra