Re: Hello World Android App
There is a lot of discussion of this application at Hello World on the developer.android.com site. They also have further discussion in the "Dev Guide" section of the site. For example the description of Activities is extensive.
The lack of a main() method (something Android apps share with Java applets now fading from memory) follows from the lifecycle that Acivities follow. This is described in the API docs for Activity.
I would highly recommend this site for the information you need to actually understand the code, rather than blindly copy/pasting it or relying on 50 word glosses to provide a kinda/sorta explanation.
Re: Hello World Android App
Thanks for the reply. I also have another question, and as it is regarding this code I thought I'd ask it in this thread.
I know that the constructor of the TextView class requires a Context object and I know that the HelloAndroidActivity class inherits from Activity which inherits from Context.
I dont think I properly understand the 'this' keyword. As the constructor requires a Context OBJECT by putting 'this' are we referring to an INSTANCE of our HelloAndroidActivity class?
Re: Hello World Android App
I ask this because I havnt actually created an instance of my class so how can I be referring to an INSTANCE of my HelloAndroidActivity class?
Re: Hello World Android App
Quote:
Originally Posted by
TP-Oreilly
As the constructor requires a Context OBJECT by putting 'this' are we referring to an INSTANCE of our HelloAndroidActivity class?
Yes, exactly. You are referring to *the* instance being constructed...
Quote:
I ask this because I havnt actually created an instance of my class so how can I be referring to an INSTANCE of my HelloAndroidActivity class?
That's a good question, because you are only part way through constructing the HelloAndroidActivity instance. Couldn't it be dangerous to pass around references to it at this point?
The answer is a bit subtle and has an implication. When a constructor gets called the first thing it does is call the super class constructor. If you don't actually have a constructor then an empty no argument one will be created for you. If your constructor does not actually begin with super() to initialise the superclass then a call to super() will be inserted for you. (A constructor can begin by invoking a different constructor - but then that one must call super()).
One way or another, by hook or by crook, the superclass will be initialised before your class's constructor gets under way. Therefore you can pass 'this' to the TextView constructor that wants a Context instance. The TextView only wants a Context instance and that "aspect" of your HelloAndroidActivity has been fully initialised.
(I posted a description of constructor rules here: http://www.javaprogrammingforums.com...html#post54597 The need to get an instance into a usable state lies behind why Java does things this way.)
-----
Passing this around depends critically on TextView not doing something silly like casting the 'this' you send it to a subclass like HelloAndroidActivity and calling methods etc. Because you haven't finished setting the instance up. It should only call Context methods. (Your instance will be in some state or other, but the results may well be surprising.) That is one reason why people will look at you sideways if you create a class but then want other methods to cast instances of it to various subclasses: that technique may well be dangerous and is certainly trickier than it looks.
Re: Hello World Android App
Thank you very much for your detailed reply.
Re: Hello World Android App