Is onCreate called when an Activity object is created?

前端 未结 4 1953
庸人自扰
庸人自扰 2020-12-14 22:45

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

相关标签:
4条回答
  • 2020-12-14 23:06

    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 ,

    0 讨论(0)
  • 2020-12-14 23:08

    According to the developer.android.com the onCreate get called when activity is started. see details here

    States of an activity

    0 讨论(0)
  • 2020-12-14 23:19

    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.

    0 讨论(0)
  • 2020-12-14 23:25

    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

    0 讨论(0)
提交回复
热议问题