Can anyone explain to me the difference between an "Activity" and an "Intent" on the Android platform?
An intent is to perform an action on the screen. It is mostly used to start activity, send broadcast receiver,start services and send message between two activities. There are two intents available in android as Implicit Intents and Explicit Intents.
An activity provides the window in which the app draws its UI. This window typically fills the screen, but may be smaller than the screen and float on top of other windows. Generally, one activity implements one screen in an app.
Android Intent is the message that is passed between components such as activities, content providers, broadcast receivers, services etc. It is generally used with startActivity() method to invoke activity, broadcast receivers etc. The dictionary meaning of intent is intention or purpose.
In very simple language, Activity
is your user interface and whatever you can do with a user interface. When you move from one user interface, you need to launch that new user interface with an Intent
. The Intent
is your event that is passed along with data from the first user interface to another.
Intents can be used between user interfaces and background services too. Also an Intent
is passed when you want to broadcast data to all activities and background services.
Intent
lives as an object, activity lives with a face and interactions. Hope it has been helpful.
Existing answers are fine but here is a really basic definition of the two with some links.
Activity
An application component for displaying a user interface. The activity class is where all user interactions are handled (button presses, list selections). An activity specifies a layout to represent it on screen.
Intent
An intent is a system message. It can be broadcast around the system to notify other applications (or your own!) of an event, or it can be used to request that the system display a new activity.
If all you know about Intents, is when you use them to start a new activity, then I can understand your confusion.
In the simplest case, you start a new activity like this:
Intent intent = new Intent(this, SomeOtherActivity.class);
startActivity(intent);
It sure looks like you are starting an activity, and the activity that you are starting is "intent". But what you are really doing is calling the method startActivity()
and you are passing it a container called intent. That container tells startActivity()
what to do.
You can see it more clearly when you are passing data to a new activity
Intent intent = new Intent(this, SomeOtherActivity.class);
startActivity(intent);
intent.putExtra("ANIMAL_TYPE", "unicorn");
intent.putExtra("ANIMAL_COLOR", "ruby");
startActivity(intent);
Now when you call startActivity()
, it looks at intent and knows that it needs start the SomeOtherActivity
class. Also, in the SomeOtherActivity
class, you can access those passed key/value pairs from the intent like this:
Bundle extras = getIntent().getExtras();
if(extras !=null) {
String animal = extras.getString("ANIMAL_TYPE");
String animalColor = extras.getString("ANIMAL_COLOR");
}
These are different classes which can't be interchanged in any way. The expected use of Activity
subclasses is to control the contents and behaviour of the application window. Intent
s, on the other hand, are simple data interchange structures often used for launching new Activity
'es and passing data to them, but they have also other uses.
The Activity class takes care of creating a (full-screen or floating) window for you in which you can place your UI-elements, so Activities interact with the user.
Intents are mostly used when you want to switch from one view (i.e. one Activity) to another one.
When you're currently in ActivityOne.class and you call:
Intent i = new Intent(this, ActivityTwo.class);
startActivity(i);
then ActivityTwo will be shown to the user.
Though there are many good explanations here, I would like to give my own view with respect to Activity
and Intent
. Activity
is a UI component which you see on your screen. An Intent
is a message object which is used to request an action from the same/different app component.
Reference : Intent, Activity
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With