Super.onCreate (savedInstanceState);

As a newbie, I have a very simple question: I created an Android application project in Eclipse, in MainActivity.java> onCreate() it calls super.onCreate(savedInstanceState) .

Can someone explain what is the purpose of the above line?

+69
android super oncreate instance
03 Feb '13 at 11:19
source share
4 answers

Each action you do is triggered by a series of method calls. onCreate() is the first of these calls.

Each of your actions extends android.app.Activity either directly or by subclassing another subclass of Activity .

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

When we redefine a method, we have the opportunity to completely replace the method in our class or extend the existing method of the parent class. super.onCreate(savedInstanceState); calling super.onCreate(savedInstanceState); , you will let Dalvik VM execute your code in addition to the existing code in the onCreate () of the parent class. If you leave this line, only your code is executed . Existing code is completely ignored.

However, you must include this super call in your method, because if you do not, the onCreate() code in the Activity will never run, and your application will run in all types of problems, such as the lack of an assigned Context (although you will end up in SuperNotCalledException before you have the opportunity to find out that you have no context).

In short, Android's own classes can be incredibly complex. The code in class classes handles things like drawing a user interface, cleaning a home, and preserving life cycles of activity and applications. super allows developers to run this complex code behind the scenes while providing a good level of abstraction for our own applications.

+131
03 Feb '13 at 11:30
source share

The Derived class onCreate(bundle) method must call the superclass implementation of this method. If you do not, an exception will be thrown SuperNotCalledException

In Java When You Perform Inheritance If you want to override a superclass method and want to execute the class method described above, in this case we use super.methodname() in the override method of the derived class;

The Android class works the same. You are extending the Activity class, which has the onCreate(Bundle bundle) method, in which meaningful code is written. So, to execute this code in our specific activity. You should use super.onCreate(bundle) .

This is code written in the Activity class onCreate() , and perhaps the Android Dev team will add even more meaningful code to this method, so you should always call it 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; /** * Control 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 allows * you to prevent your 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 theme. */ 

It also supports the mCalled variable, which means that you called 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 the 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

+4
03 Feb '13 at 11:29
source share

Because after super.onCreate () it will reach the Activity class (the parent class of any activity) to load the saved InstanceState, and we normally do not set any state of the saved instance, but the android infrastructure was made in such a way that we should call it.

+1
Feb 03 '13 at 11:33
source share

This is the information that you want to return to the application through onCreate (), if the action is destroyed and restarted due to some implicit reason (for example, not because the user clicked the "Back" button). The most common use of onSaveInstanceState () is to handle screen rotations, since by default, actions are destroyed and recreated when the user leaves the G1 keyboard.

The reason for calling super.onCreate (savedInstanceState) is that your code will not compile otherwise .; -)

+1
Jul 15 '15 at 17:32
source share



All Articles