Nav
Nav

Reputation: 10412

Is there any difference between these two types of intent declarations?

step1 > Intent i = new Intent(home.this, secondactivity.class);

step2> Intent i = new Intent("android.intent.action.secondactivity");

I am a little confused as to what difference does it make when creating an intent like in step 1 or step 2

In the 1st one I am specifying the current instance of the class from which the activity should go to the next activity e.x on a button click in the home activity then it will go to the second activity. In the 2nd one I am not doing that.

Does it makes any difference or both are same ?

Upvotes: 0

Views: 1044

Answers (3)

Pal Szasz
Pal Szasz

Reputation: 3225

They are different:

In the first variant you explicitly say which component/activity to execute, so it's guaranteed that you will execute that component. In that case your second activity doesn't even need to have an intent-filter specified.

In the second variant you specify an ACTION name. For that to work, your second activity must have an intent filter with the same action. However note that if by any chance there is another application with an activity which has an intent filter with the same action name, then when launching the activity, the user will get an activity picker dialog to choose which one to use.

You should use the first variant if you ALWAYS want to execute your second activity on button click.

You should use the second variant if you want to create an API, i.e. you want other applications to be able to hook into your application workflow.

Upvotes: 3

Diego Torres Milano
Diego Torres Milano

Reputation: 69198

Your second intent is wrong, "android.intent.action.secondactivity" is not an action.

Upvotes: 0

Dany's
Dany's

Reputation: 928

You can use step2 when you call your SecondActivity from somewhere outside. Let's say from a broadcast Receiver, or from a Service.

Upvotes: 0

Related Questions