Android - Activity Constructor vs onCreate
Asked Answered
R

4

96

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?

Roughandtumble answered 21/7, 2010 at 17:44 Comment(1)
What about the fact that Android can destroy/recreate your Activity at any time? You don't know if constructor will be called then and even if - which constructor will be called... (the same applies to Fragments and this is why every Fragment has to implement an empty default constructor).Picul
I
38

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.

Isagoge answered 21/7, 2010 at 20:26 Comment(5)
onCreate() prevents you from using final fields.Ticker
But OnCreate is not called only once, am I wrong? When I change screen orientation and back by my hand, each time the activity reloads, oncreate is calledSkell
@Skell onCreate is called only once per instance I believe. When rotating the device, that instance of the Activity is destroyed and a new one is created, calling onCreate. That said, I'm pretty certain you can't instantiate final fields in onCreate because Java doesn't know onCreate is only going to be called once (and indeed, you could call it again yourself in your code - bad things will happen, but it'll still compile) and so the only way to instantiate final fields would be in the constructor.Ruffi
When does OnCreate event start to run exactly? When I set a breakpoint at beginning of OnCreate event so app run and loads activity to screen then breakpoint activates and the app goes to pause. I need an event about the activity that activates exactly before activity initialized and launched.Henceforth
@Cheryl Simon, You told you never create an activity directly, then who creates the activity?Quicksilver
K
7

A good reason for putting things in the constructor as Gili's comment had stated is the use of final fields.

However, if you initialize things in the constructor, then the lifespan of the object will be a little bit longer, though I don't think by much because the onCreate would be called shortly thereafter.

Although it's against my ideal, I do avoid the constructor for initialization of the activity members and rely on onResume() and onPause() for resources that my app is dealing with.

For onCreate() I usually use it to do view mapping to local variables. Though android-annotations already does that for me so I rarely have an onCreate() method for my Activity. I still use it in Service though.

However, if you look at the members you may be initializing

  • they would have a "close" method that you have to invoke at the proper time (onResume or onPause)

  • they would be part of the view which means it needs to be initialized then onCreate needs to be called

  • they are constants which don't need to be put in the constructor anyway, just a static final would do. This includes Paint and Path constants which can be initialized by a static block

Kelly answered 19/7, 2013 at 21:46 Comment(2)
What do you mean the lifespan of the object will be a little bit longer? I what way? Since if you moved these initalisations into the onCreate, for example, that still takes the same time. There is no difference in lifespan that I can determine. Can you expand on this a little more please as I feel, as a relative newcomer, I might be missing something crucial here.Besprent
@Besprent by longer Archimedes is just saying that the constructor gets called before onCreate() and so whatever is done there will have persisted (slightly) longer than otherwise by the time the activity is destroyedRhianna
A
6

I am now on a case that needs to override the constructor. In fact, I have some activities that have the same structure. So instead of creating many activities, I'll create one "Master" activity and the others will inherit this one. So I need to override the constructor of the child activity to be able to initialize some variables that will be used in the oncreate methods.

In two words, the constructor makes you simulate a "masteractivity" that can be reused by inheritance!

Agronomy answered 13/1, 2012 at 23:46 Comment(2)
I know this is old but what is the benefit here over just implementing the super field instantiation in it onCreate(). You'll be calling super.onCreate() from the child anyway.Actinomycosis
So just by passing diffrent Values to same KEY in bundle or intent while launching activity and thus using same Activity you can determine what to show in Activity depending on value received. What is the specific reason you went for construtors? Or else keeping the non-changing part of Activity common and for rest of the changing part you could have created Fragments.Imperial
H
0

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

Hubert answered 21/7, 2010 at 18:39 Comment(5)
Can you elaborate on this more? What you describe sounds interesting, but it is a little vague. Thanks!Roughandtumble
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?Hubert
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.Hubert
I'm going to say, that doesn't make sense to me @Pentium, not without a code example.Atwell
I think one advantage of doing things via constructor is to have a "template" activity which can take in parameters that an inherited class can take advantage of. For example if you have two activities which only differ in some of the properties e.g. R.id.cameraSurface, R.id.videoSurface then you can create a constructor that will take in a parameter call it AbstractResourceActivity and then you have CameraActivity which is registered on the manifest that extends AbstractResourceActivity passing in the resource ID.Kelly

© 2022 - 2024 — McMap. All rights reserved.