0

Well, latest question I posted was my first post, so I admit I didn't summarize my question well . So I am going to ask a question again.

override fun onCreate(savedInstanceState: Bundle?) {
    setContentView(R.layout.activity_main)
    super.onCreate(savedInstanceState)         
}

I know onCreate() method in superclass(AppCompactActivity) create Activity before starting it. Then, isn't it necessary for "super.onCreate" to come before the initialization of View(like setConentView)?? But, When I put the superclass method implementation in the far below, it didn't make problem (code working normally), Why???

AsthaUndefined
  • 1,111
  • 1
  • 11
  • 24
백근우
  • 3
  • 5

1 Answers1

0

It is not necessary to call onCreate first because major chunk of initialization already happens in the constructor of the Activity. onCreate method is provided to the developer to do any app specific initialization.

If you look at the internals of onCreate, it does bunch of stuffs like set the action bar, initialize fragments, apply the theme and other support library related stuffs but the parent view of the activity (called decor view) is already initialized. By calling setContentView we are just inflating the passed layout id and setting it as a child of decor view.

Try running the application without calling setContentView. The application will flawlessly run with the action bar on top but you won't see any view added.

To summarize, super.onCreate and setContentView are two independent operations.

Binary Baba
  • 1,953
  • 2
  • 16
  • 23
  • So you mean function of onCreate in superclass is just adding a titlebar and actionbar (in this case, set the parent view???)??? – 백근우 Oct 27 '18 at 08:02
  • ah.. but I can't understand exactly. if the function of onCreate in superclass is set parent view, isn't it necessary it comes before any other inflation of view? because we need parent view first to inflate the child view – 백근우 Oct 27 '18 at 08:07
  • we can say that parent view is inflated (or created) even before Activity's `onCreate` is called. – Binary Baba Oct 27 '18 at 09:05
  • well, if parent view is inflated before the call of overrided onCreate, what is the exact function of 'super.onCreate(...)' ?? – 백근우 Oct 27 '18 at 09:08
  • I have edited my answer, actually it does bunch of other stuffs. But the point being the initialization of the parent view take place before `onCreate` is called. That's why you can call `setContentView` after `onCreate`, since both are independent calls. – Binary Baba Oct 27 '18 at 09:14
  • hmm so.. you mean it is a rule that order of "which place super.onCreate() come " is not important but at least we need the code inside the overrided onCreate for the method working?? – 백근우 Oct 27 '18 at 09:21
  • ]I have understood that super.onCreate create Activity. But If it does, I thought super.onCreate() code had to come the top of the inside of overrided onCreated – 백근우 Oct 27 '18 at 09:23
  • Since some of the activity creation happens before `onCreate` is called. We can say that by calling `super.onCreate`, remaining part of activity creation is being executed. – Binary Baba Oct 27 '18 at 09:28
  • Does that answer your question? – Binary Baba Oct 27 '18 at 12:12
  • I think there are much more things for me to learn.. Anyway, I 'm glad to have enough question and appreciate your sincere Help.!!!!!! – 백근우 Oct 27 '18 at 14:11
  • + Can you tell me Which part I have to refer to get specific things?? Activity LifeCycle?? – 백근우 Oct 27 '18 at 14:14
  • Sorry, I didn't get your question. Do you want to learn about activity lifecycle? – Binary Baba Oct 28 '18 at 05:50
  • For the starters, following explains very well about activity lifecycle https://stackoverflow.com/questions/8515936/android-activity-life-cycle-what-are-all-these-methods-for – Binary Baba Oct 29 '18 at 02:02