开发者

Noob question about "Hello World" Android tutorial

开发者 https://www.devze.com 2023-04-03 21:36 出处:网络
Just getting started with Android development and Java.开发者_Python百科So, here\'s the code I\'m working with:

Just getting started with Android development and Java. 开发者_Python百科So, here's the code I'm working with:

package com.example.helloandroid;

import android.app.Activity;
import android.os.Bundle;

public class HelloAndroidActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

What is the purpose of declaring the onCreate() method here:

public void onCreate(Bundle savedInstanceState) {

Then using super to call the onCreate() method here:

super.onCreate(savedInstanceState);

Doesn't this mean that you are calling the onCreate() method from the Activity class rather than the HelloAndroidActivity class? If so, what is the point of declaring a method with the same name in the HelloAndroidActivity class?

Thanks for any clarification.


In your example, HelloAndroidActivity is a class that inherits from Activity. You are then overriding the base class (Activity)'s onCreate method. Overriding occurs when you define a method in a derived class with the same signature as an existing method in the base class. This means that when I call onCreate on an instance of HelloAndroidActivity, I will execute the HelloAndroidActivity version, and not the base class (Activity)'s version.

The instruction super.OnCreate(savedInstanceState) in the overridden version is explicitly calling the base class's version of the method. What this means is you want HelloAndroidActivity.onCreate to first execute the base class's implementation, then run some more code.


Take the following examples to illustrate this behavior (assume the method Output just outputs something to the screen):

1.

class A
{
    public void DoSomething()
    {
        Output("A");
    }
}

In this case, calling A.DoSomething() will output "A".


2.

Assume we still have the class A defined as above, and the following:

class B extends A
{
}

In this case, calling B.DoSomething() will also output "A".


3.

Assume we still have the class A defined as above, and the following instead:

class B extends A
{
    @Override
    public void DoSomething()
    {
        Output("B");
    }
}

Now, calling B.DoSomething() will output "B".


4.

Assume we still have the class A defined as above, and now this instead:

class B extends A
{
    @Override
    public void DoSomething()
    {
        super.DoSomething();
        Output("B");
    }
}

Now, calling B.DoSomething() will output "A" and then "B".


You are creating a class that inherits from Activity; but you have to define its behaviour, so you tell your class, when created, to call base class to complete all the things that must be done (super.onCreate()), then you set your layout to show your screen/app (setContentView()).

One thing more: take a look at @Override defined right before public void onCreate(Bundle savedInstanceState): override means that you're extending base method to let your derived class do all the work of the base one, including (after) your logic.


Question: What is the purpose of declaring the onCreate() method here? Answer: The oncreate() method is analogous to main(String args[]) in ususal JAVA. The oncreate activity is called when your Activity starts. So all the initialisations should be done in this method.

You write: public class HelloAndroidActivity extends Activity This means that you are creating a class - "HelloAndroidClass" that inherits from class = "Activity".

You write: super.onCreate(savedInstanceState); Java uses the keyword "super" to call constructors from parent class. You are using the concept of overriding here. When you run your code, the oncreate method of base class "HelloAndroidActivity" is executed and not that of parent class - "Activity class".

So you code will first run the base class implementation of oncreate() method(super.oncreate(...)) and then the implementation of derived class.

Question: What is the point of declaring a method with the same name in the HelloAndroidActivity class? Answer: The super keyword is being used to initialize the objects. The point in redeclaring the same method and using overiding is that you are saved from rewriting the code of base class.


onCreate is sort of your main(String[] args) function in normal Java. It is where you setup your code.


From documentation for onCreate method:

Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.

See more here for activity methods, lifecycle etc.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号