Search code examples
androidandroid-activitydefaultoncreate

What is the default activity if you don't override onCreate?


I am new to both Android and Java (as well as OOP), so forgive me if this is super noobish. In going through the Android tutorial on a first app, the following code is written:

@Override
protected void onCreate(Bundle savedInstanceState){...}

I have read that @Override is overriding the default onCreate call and that, if you don't override, you can't specify which activity to use. That's all well and good, but in the interest of understanding, what exactly is the default activity if activity_main is not in the case that you don't override onCreate?


Solution

  • I think what you are asking is what does the base class onCreate method do, and what happens if you don't override it. You can look at the source code for Activity here, or the AppCompatActivity here to look at what is in the base class. Notice that a typical implementation in an app looks like

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // other custom setup code for your activity
    }
    

    That first call to super.onCreate still calls the base class onCreate method (the one you are overriding), so you're not replacing what's in the base class, but adding your customizations specific to the Activity you are creating. That call to super does a number of Activity setup things that you always have to do. It doesn't associate the activity with any layout file (which is done by the setContentView call), so it wouldn't display anything from your xml files without you adding the setContentView call. There is no default xml layout file it would use.