Curvian Vynes
Curvian Vynes

Reputation: 70

Android development: Using intent syntax

I'm learning Java and playing around with Android samples in Eclipse. I've come across code from two different sources and would like to know the difference between the following:

Intent intent = new Intent(this, SomeActivity.class);

---AND---

Intent intent = new Intent().setClass(this, SomeActivity.class);

Thanks!

(All these objects make me feel like I'm building a puzzle rather than coding. Not having much fun here...:))

Upvotes: 1

Views: 1930

Answers (1)

kriz
kriz

Reputation: 611

In the first case you create the Intent with the class data. In the second case you create an empty Intent and set the class data after. The result is the same, according to the android source code.

The constructor:

public Intent(Context packageContext, Class<?> cls) {
    mComponent = new ComponentName(packageContext, cls);
}

The setClass method:

public Intent setClass(Context packageContext, Class<?> cls) {
    mComponent = new ComponentName(packageContext, cls);
    return this;
}

Upvotes: 2

Related Questions