백근우
백근우

Reputation: 3

Questions about onCreate()?

package com.example.samsung.myapplication

import android.support.v7.app.AppCompatActivity
import android.os.Bundle

    class MainActivity : AppCompatActivity() {

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

    }

According to developer docs, when I override callback function of Activity Class, it is essential to call through superclass implementation.

So these are my 2 Questions :

  1. Is it ok to callback superclass method after initiallization of Activity? Can you tell me reason wh?y?? ex) setContentView(....) .... view.adapter = ... (just guess there are several code above) super.onCreate(...)

  2. What is main function of onCreate in superclass ??

Upvotes: 0

Views: 53

Answers (2)

Domin
Domin

Reputation: 1145

onCreate is method that is being called when activity is called. In this method you define e.g layout which is to be loaded and initialize e.g views. To sum up, this method creates a View

as you wrote view.adapter you call the reference to instance of view.In most cases you call view in fragments to e.g set layout or again initialize view. Take in care that in Activity we override onCreate and in Fragments we call onCreateView and return View.

As superclass we mean the class we override as you wrote and if you want to create an activity in most cases must extend AppCompatActivity() (braces in Kotlin, you extends by adding colon after declaring activity name, e.g class App : AppCompatActivity())

I don't know if it's right answer for your question because I didn't understand it at all. I hope it will help you. If you have any questions about superclasses etc. you can easilly Google it and in most cases you'll get easilly answer.

Upvotes: 0

m0skit0
m0skit0

Reputation: 25873

Is it ok to callback superclass method after initiallization of Activity? Can you tell me reason wh?y?? ex) setContentView(....) .... view.adapter = ... (just guess there are several code above) super.onCreate(...)

No because most calls to Android API before calling super.onCreate() will fail with an exception because mCalled has not been set to true (see Activity source code for reference)

What is main function of onCreate in superclass ??

To understand the function of onCreate() you should understand the Activity lifecycle and the role it plays there.

Upvotes: 1

Related Questions