Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

super.onCreate(savedInstanceState);

I have created an Android Application Project and in MainActivity.java > onCreate() it is calling super.onCreate(savedInstanceState).

As a beginner, can anyone explain what is the purpose of the above line?

like image 565
Pramod Avatar asked Feb 03 '13 11:02

Pramod


People also ask

What is super onCreate savedInstanceState?

By calling super. onCreate(savedInstanceState); , you tell the Dalvik VM to run your code in addition to the existing code in the onCreate() of the parent class. If you leave out this line, then only your code is run. The existing code is ignored completely.

What is the purpose of super onCreate () in android?

Q 9 – What is the purpose of super. onCreate() in android? The super. onCreate() will create the graphical window for subclasses and place at onCreate() method.

What is used of onCreate () method?

onCreate(Bundle savedInstanceState) Function in Android: When an Activity first call or launched then onCreate(Bundle savedInstanceState) method is responsible to create the activity.

What does onCreate mean?

onCreate is used to start an activity. super is used to call the parent class constructor. setContentView is used to set the xml.


2 Answers

Every Activity you make is started through a sequence of method calls. onCreate() is the first of these calls.

Each and every one of your Activities extends android.app.Activity either directly or by subclassing another subclass of Activity.

In Java, when you inherit from a class, you can override its methods to run your own code in them. A very common example of this is the overriding of the toString() method when extending java.lang.Object.

When we override a method, we have the option of completely replacing the method in our class, or of extending the existing parent class' method. By calling super.onCreate(savedInstanceState);, you tell the Dalvik VM to run your code in addition to the existing code in the onCreate() of the parent class. If you leave out this line, then only your code is run. The existing code is ignored completely.

However, you must include this super call in your method, because if you don't then the onCreate() code in Activity is never run, and your app will run into all sorts of problem like having no Context assigned to the Activity (though you'll hit a SuperNotCalledException before you have a chance to figure out that you have no context).

In short, Android's own classes can be incredibly complex. The code in the framework classes handles stuff like UI drawing, house cleaning and maintaining the Activity and application lifecycles. super calls allow developers to run this complex code behind the scenes, while still providing a good level of abstraction for our own apps.

like image 88
Raghav Sood Avatar answered Nov 15 '22 12:11

Raghav Sood


*Derived class onCreate(bundle) method must call superclass implementation of this method. It will throw an exception SuperNotCalledException if the "super" keyword is not used.

For inheritance in Java, to override the superclass method and also to execute the above class method, use super.methodname() in the overriding derived class method;

Android class works in the same way. By extending the Activity class which have onCreate(Bundle bundle) method in which meaningful code is written and to execute that code in the defined activity, use the super keyword with the method onCreate() like super.onCreate(bundle).

This is a code written in Activity class onCreate() method and Android Dev team might add some more meaningful code to this method later. So, in order to reflect the additions, you are supposed to call the super.onCreate() in your Activity class.

protected void  onCreate(Bundle savedInstanceState) {     mVisibleFromClient = mWindow.getWindowStyle().getBoolean(     com.android.internal.R.styleable.Window_windowNoDisplay, true);     mCalled = true; }  boolean mVisibleFromClient = true;  /**  * Controls whether this activity main window is visible.  This is intended  * only for the special case of an activity that is not going to show a  * UI itself, but can't just finish prior to onResume() because it needs  * to wait for a service binding or such.  Setting this to false prevents the UI from being shown during that time.  *   * <p>The default value for this is taken from the  * {@link android.R.attr#windowNoDisplay} attribute of the activity's theme.  */ 

It also maintains the variable mCalled which means you have called the super.onCreate(savedBundleInstance) in your Activity.

final void performStart() {     mCalled = false;     mInstrumentation.callActivityOnStart(this);     if (!mCalled) {         throw new SuperNotCalledException(             "Activity " + mComponent.toShortString() +             " did not call through to super.onStart()");     } } 

See source code here.

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/app/Activity.java#Activity.onCreate%28android.os.Bundle%29

like image 25
Ajay S Avatar answered Nov 15 '22 14:11

Ajay S