Is onCreate()
called when a class object that extends Activity
is created? Or is it only called when an activity is started, for example over startActivity(...)
?

- 722
- 1
- 7
- 22

- 4,388
- 17
- 50
- 76
4 Answers
To answer you question, for a class that extends activity, if you try to instantiate that Activity by normal means ( MyActivity ma = new MyActivity(); ) the onCreate() method WILL NOT be called. Only if you start the Activity with an intent will the method be called.

- 14,515
- 6
- 62
- 88
-
What happens when recreate() is called? – Karue Benson Karue Jul 15 '17 at 22:07
-
For that matter MyActivity ma = new MyActivity(); does this have any use or significance? I think any operation related to activity happens when an activity is started through an Intent. – Sreekanth Karumanaghat Dec 25 '19 at 08:40
Each activity in an application goes through its own lifecycle. Once and only once when an activity is created, is the onCreate() function executed.
Check this Activity Life Cycle

- 10,234
- 7
- 48
- 75

- 11,049
- 5
- 49
- 66
i think that in Android , you cant write something like this :
AClassThatExtendedAnActivity instance = new AClassThatExtendedAnActivity();
the only way that you can use to launch an activity is passing with an intent to start your activity .
the creation of the instance is encapsulated on the super.onCreate(savedInstanceState);
when you override the method onCreate(Bundle savedInstanceState);
Regards ,

- 24,001
- 13
- 56
- 83
-
But we have learned in OOPs that only way to access the methods of a class is through the object of the class, so isn't this against that? Please throw some light into this. – Sreekanth Karumanaghat Dec 25 '19 at 08:40
-
1We do not call the onCreate() method ; it is called automatically when you start an Activity from intent. – Houcine Dec 25 '19 at 19:46
-
I understand that, but suppose there is a public method. methodXYZ() in Activity A and I can call this method from another activity say Activity B in its onCreate() so is there any use for this kind of pattern? – Sreekanth Karumanaghat Dec 26 '19 at 14:32
-
This kind of pattern is useless and you should avoid it. in order to call a public methode from another activity, i think it depends on what do want to do in that method, i.e you can use startActivityForResult, or use callbacks pattern to communicate between the two activities. I think you should throw an eye on this thread : https://stackoverflow.com/questions/19666572/how-to-call-a-method-in-another-activity-from-activity. – Houcine Dec 27 '19 at 02:00
-
I understand that it is useless, but if we are coming from the OOPs world, isn't that we have been taught? I think I never read about a call back method in the OOPs concepts, so if I were a newbie into android say with an OOPs background, then is there any intuitive way I could understand these concepts intuitively? – Sreekanth Karumanaghat Dec 27 '19 at 06:37