Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialHudson Kim
2,965 PointsIntent what?
I'm am completely lost in what Ben was saying in the video. To be exact I don't understand what the second parameter in the intent method is used for or why it's needed as well as how this translates into connecting this to another activity. Can someone please help me sort out this intent jumble.
2 Answers
Kyle Knapp
21,526 PointsIntent intent = new Intent(this, StoryActivity.class);
This statement creates a new instance of the Intent constructor by passing it the current context as the first parameter, and the specific class we want to target as the second parameter.
So the basic structure is:
Intent myIntent = new Intent(MyContext, MyClass.class);
Back to the original example:
Intent intent = new Intent(this, StoryActivity.class);
this
in the first parameter defines the current activity as the context, and StoryActivity.class
in the second parameter specifies which class in the context that the new Intent object is targeting.
If your context has multiple classes, your new Intent object won't know which class to use unless you specify it with the second parameter. So, we pass in StoryActivity.class
to let our program know that we want to target the StoryActivity class.
Ari Misha
19,323 PointsHello there! So Kyle Knapp provided a great answer regarding Intents in Android Native. This one is just a follow up to the above answer. An Intent is nothing but a messaging Object that can be used to request an action from another component of another app. The basic syntax would look like this (the snippet code is in Kotlin, only 'coz I mostly use Kotlin for my Android apps):
val myIntent: Intent = Intent(Context, Class)
In the above line of code, Context represents the current context and the Class is another component. Do lemme know if you've any queries or doubt the answer.
~ Ari