Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Hello World Android App

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hello World Android App

    Hello, I following a "Hello World" tutorial for android development and I want to understand how the code works exactly, instead of just using it without understanding it, heres the code:

    package apps.helloAndroid;
     
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;
     
    public class HelloAndroidActivity extends Activity {   
     
    	/** Called when the activity is first created. */   
    	@Override   
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);       
    		TextView tv = new TextView(this);       
    		tv.setText("Hello, Android");       
    		setContentView(tv);   
    	}
     
    }

    What are we extending into our class by extending Activity and what do we use of it. And, where's the main method?


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default 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.

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    JavaPF (January 22nd, 2012)

  4. #3
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?
    Last edited by TP-Oreilly; January 22nd, 2012 at 01:18 PM.

  5. #4
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  6. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Hello World Android App

    Quote Originally Posted by TP-Oreilly View Post
    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...

    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.
    Last edited by pbrockway2; January 22nd, 2012 at 03:04 PM.

  7. #6
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hello World Android App

    Thank you very much for your detailed reply.

  8. #7
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Hello World Android App

    You're welcome.

Similar Threads

  1. Hello World!
    By i_masyhudi in forum Member Introductions
    Replies: 1
    Last Post: July 27th, 2011, 02:50 AM
  2. Hello World
    By av8 in forum Member Introductions
    Replies: 1
    Last Post: June 27th, 2011, 10:05 AM
  3. Hello world!
    By SamirGunic in forum Member Introductions
    Replies: 1
    Last Post: April 14th, 2011, 06:57 AM
  4. hello world
    By WiFiLeech in forum Member Introductions
    Replies: 2
    Last Post: August 31st, 2010, 07:33 AM
  5. Hello World !
    By ArunUOM in forum Member Introductions
    Replies: 2
    Last Post: June 28th, 2010, 03:29 AM