Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Activities (and many other things in Android) are started using an object called an Intent. The intent of this Intent is to start another Activity. :)
-
0:00
So, when you as a person intend to do a task, it means that you've thought about
-
0:04
what you wanna do, and you're intent is to do it.
-
0:07
You just need to start and maybe you need a little help.
-
0:10
In Android, it's the same basic idea,
-
0:12
but it's wrapped up in an intent object that we can use in code.
-
0:16
An intent is what we use when we intend to do something in an Android app.
-
0:20
We usually use them to start a new task, and
-
0:22
that task can be something like starting a new activity, or
-
0:25
maybe handing something off to another app like a video player, or a messaging app.
-
0:30
Back in our code, let's declare a new intent object named, simply, intent.
-
0:35
We'll set it equal to a new Intent, with parentheses, and add a semicolon.
-
0:40
Now, there are a few different constructors we can use.
-
0:43
So, if we go back between the parentheses, we can hit Cmd+P on a Mac, or
-
0:47
Ctrl+P on Windows, to bring up some help.
-
0:50
We're going to start a new activity, which is defined in a class file.
-
0:54
Thus, we want the fourth option shown here, that accepts the current context,
-
0:58
and a class as a parameter.
-
1:01
Remember that context is really important in Android.
-
1:04
We discussed it briefly before, but it is essentially the specific context or
-
1:08
environment within which our code is running.
-
1:11
This code is in an activity, and
-
1:13
the activity class is a sub class of the context class.
-
1:16
So we can use the current activity as our context.
-
1:19
This is easily achieved with the this keyword.
-
1:22
And in this case, it's now referring to this current class.
-
1:25
There are other ways to access the context.
-
1:27
For example, we could type getApplicationContext,
-
1:30
which is a method available in the activity class.
-
1:33
But let's undo that, go with this, and now for
-
1:36
the second parameter, we want the activity class that we want to start.
-
1:41
So we type StoryActivity and then we type .class to reference the actual class.
-
1:47
When our app is actually running, it needs the class object
-
1:50
rather than simply the name which is why we use this class property.
-
1:53
Okay, next, just one more line of code.
-
1:55
We need to express this intention somehow.
-
1:58
This is often done using the method from the activity base class called,
-
2:01
startActivity.
-
2:04
And look at that.
-
2:05
It takes an intent as its parameter.
-
2:07
Type that and try it out, and see what happens.
-
2:12
Okay, we can type a name again.
-
2:17
Hit enter.
-
2:18
Start the adventure.
-
2:19
All right, and we're taken to our new blank story activity.
You need to sign up for Treehouse in order to download course files.
Sign up