0
package com.example.mytests;

import android.app.Activity;

public class MainActivity extends Activity  {}

For the code above, even though I do not implement OnCreate() method I get no errors and program works. Should't I get an error that I should override it?

Shibli
  • 5,879
  • 13
  • 62
  • 126

4 Answers4

2

No. You will get error if you override onCreate and forget to call super.onCreate first thing.

In your case just Activity.onCreate() gets called. It can't really give an error, unless they in Google break something severily which they do not. If it gave Android wouldn't work because the same error would come from super.onCreate()

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
1

No, you don't need to override it. But if you DO override it, you MUST call super.onCreate().

The idea is that no matter what, the default implementation of onCreate() must be called, which is also the case if you do not override it at all.

npace
  • 4,218
  • 1
  • 25
  • 35
1

onCreate is one of the Life Cycle methods of the Activity

Reason we implement the onCreate because some where we need to set the content view to the activity. For that reason we choose onCreate for setContentView() and it is the first life cycle method to be called. and if you override the onCreate dont forgot to call the super.onCreate()

why super.onCreate

Community
  • 1
  • 1
Jagadesh Seeram
  • 2,630
  • 1
  • 16
  • 29
0

You are extending class which has already defined onCreate method so overriding indicates to use your behavior.otherwise super class behavior will b used.this is same as extend thread class and not override run method.here do nothing run method will b invoked when u call start method on thread . You might be confused in implementing and extending interface or class.

Manmohan Badaya
  • 2,326
  • 1
  • 22
  • 35