nullByteMe
nullByteMe

Reputation: 6391

Why do android methods accept View objects, but the objects are unused?

I'm extremely new to android development, and still a novice in java development; but anyway I'm trying to learn how to develop android apps and I'm trying to understand how everything works together with java and all the resource xml data.

As I was reading some sample android code from http://developer.android.com/training/basics/activity-lifecycle/index.html I noticed that they have a lot of methods like this:

public void startDialog(View v) {
    Intent intent = new Intent(ActivityC.this, DialogActivity.class);
    startActivity(intent);
}

Why do these methods accept View objects, but appear to never be used in the actual method? How am I to understand what is happening here?

Upvotes: 1

Views: 111

Answers (1)

stinepike
stinepike

Reputation: 54682

The View v indicates from which view the method is trigerred. For example your startDialog method can be used in two or more views' android:onClick attribute. Then you can use like

public void startDialog(View v) {
    switch(v.getId()){
    case R.id.view1:
         // do something
         break;
    case R.id.view12:
         // do something
         break;
    }
}

Upvotes: 5

Related Questions