Intents
Last updated
Was this helpful?
Last updated
Was this helpful?
To start a new Activity we use an Intent. Intents are messaging objects which means that they can also be used to pass data from one Activity to another. Intents also have other uses such as starting different types of app components including components from other applications, e.g. composing a new email. In this course intents will only be used to start a new Activity in the same app. In the example below an intent is created, the name Daniel and number 1 is attached and the intent is used to start SecondActivity
.
Notice the difference between MainActivity.
this
and SecondActivity.
class
. The use of the keyword this
is because that we already have a MainActivity while the keyword class
is used to denote an uninstantiated SecondActivity. Don't worry if you don't understand the difference in full, we will ask no questions regarding this during the exam.
Normally no action is needed to handle the intent in the SecondActivity
. However, if there are attached data as the case name and number above, that data is made available from the intent in the form of a Bundle
.
Note that the bundle may not exists, e.g. if no data was attached, so it is important to check if the bundle if null before it is being accessed, or the application might crash.
VIDEO