46

Can anyone explain to me the difference between an "Activity" and an "Intent" on the Android platform?

MikeN
  • 45,039
  • 49
  • 151
  • 227
Ruby
  • 752
  • 2
  • 8
  • 23

8 Answers8

67

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.

Yogesh Umesh Vaity
  • 41,009
  • 21
  • 145
  • 105
Siddharth
  • 9,349
  • 16
  • 86
  • 148
  • I want to point out something here. Imagine we have really simple android application. i.e text box says hello within this activity we can invoke this.getIntent() in case some other programs or activities send us data. So I think activity and intent are closely linked each other. (Guys please fix me if I am wrong I just start programming in Android so I am also struggle with some concepts) – Can Oct 16 '14 at 21:40
  • 1
    I dont think there is best explanation out there then this one. – Hemant Sankhla Jun 16 '22 at 06:29
25

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.

Gusdor
  • 14,001
  • 2
  • 52
  • 64
  • 2
    What difference does it make while launching an `Activity` and launching an `Intent` using `am' i.e. Activity Manager. – Krishna Oza May 05 '17 at 06:23
21

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");
}
HalR
  • 11,411
  • 5
  • 48
  • 80
4

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. Intents, 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 Vee
  • 11,420
  • 5
  • 27
  • 60
4

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.

behar
  • 41
  • 4
2

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.

enter image description here

Reference : Intent, Activity

Tom Taylor
  • 3,344
  • 2
  • 38
  • 63
0

Activity: It is like web page eg. home, contact. It contain all the UI part of the application.

Intent: You can think of Intent as a way to open another activity. It basically helps to open new activity along with data from previous activity.

0

Activities: An activity is a component that you see on a screen, with some associated logic to manage life cycle and navigation. An application will consist of several activities. When you move from screen to screen, generally you are changing Activities, replacing what is on the screen with the new contents and controller. CHECK ACTIVITY LIFE CYCLE

Contents: navigation between activities is managed by Intents. An Intent is a type of message that applications broadcast through the Android OS to interested parties on the phone.