1

I'm learning android development and I need a hint on the inner working of the whole thing.

I'm using the code showed here

In

public void sendMessage(View view) {
  Intent intent = new Intent(this, DisplayMessageActivity.class);
}

I want to know what "this" is

In the guide I read:

A Context as its first parameter (this is used because the Activity class is a subclass of Context)

What does Context class do? How is it used? Why does Activity inherit from it?

Now the main question:

If you check the whole example, they start the other activity directly from the button with the sendMessage() method. There is a way to use the onClick event listener and start the activity from there so I can do some stuff before starting the activity (like initializing some variables or so)?

And, is it really necessary editing the android manifest file by hand? They put all the stuff in there about editing the android manifest everytime you add an activity. Do I have to do that exact thing every time I add an activity? I would like to edit the AndroidManifest.xml file more conscientiously, knowing what I'm typing in and why. In that guide all is put up mysteriously and they don't explain nothing.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
Liquid Core
  • 1
  • 6
  • 27
  • 52
  • possible duplicate of [What is the meaning of "this" in Java?](http://stackoverflow.com/questions/3728062/what-is-the-meaning-of-this-in-java) – Code-Apprentice Jun 21 '15 at 16:35

1 Answers1

1

I want to know what "this" is

To understand this, see What is the meaning of "this" in Java?.

What does Context class does?

A Context is the glue between your app and the operating system. It allows you to access resources on the device, such as images and databases.

If you check the whole example, they start the other activity directly from the button with the sendMessage method. There is a way to use the onClick event listener and start the activity from there so I can do some stuff before starting the activity (like initializing some variables or so)?

android:onClick="sendMessage" in the XML for the button is a listener for the OnClick event. You can do whatever you wish in this method, including initializing variables.

And, it's really necessary editing the android manifest file by hand? They put all the stuff in there about editing the android manifest everytime you add an activity. Do I have to do that exact thing everytime I add an activity?

Yes, every activity must be registered in AndroidManifest.xml with an <activity> tag. At this point, it is probably unimportant to understand all the nuances. I suggest following the examples you see when you want to add more activities. Note that typically only one activity will have an <intent-filter>. Don't worry too much about these until you need to learn about them later.

Community
  • 1
  • 1
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268