Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is onCreate called when an Activity object is created?

Tags:

android

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(...).

like image 520
ShrimpCrackers Avatar asked Sep 19 '11 18:09

ShrimpCrackers


People also ask

What does onCreate mean?

onCreate is used to start an activity. super is used to call the parent class constructor. setContentView is used to set the xml.

What is it called when an application is onCreate?

onCreate() - called before the first components of the application starts. onLowMemory() - called when the Android system requests that the application cleans up memory.

Is onCreate only called once?

OnCreate is only called once.

What does onCreate function do?

onCreate(Bundle savedInstanceState) Function in Android: After Orientation changed then onCreate(Bundle savedInstanceState) will call and recreate the activity and load all data from savedInstanceState. Basically Bundle class is used to stored the data of activity whenever above condition occur in app.


2 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.

like image 92
SBerg413 Avatar answered Sep 18 '22 18:09

SBerg413


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 ,

like image 39
Houcine Avatar answered Sep 19 '22 18:09

Houcine