Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between activity and intent in Android?

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

like image 949
Ruby Avatar asked Apr 28 '13 05:04

Ruby


People also ask

Is Intent an activity?

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.

What is an activity in Android?

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.

What is Intent in Android with example?

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.


6 Answers

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.

like image 126
Siddharth Avatar answered Oct 15 '22 23:10

Siddharth


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.

like image 28
Gusdor Avatar answered Oct 15 '22 23:10

Gusdor


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");
}
like image 36
HalR Avatar answered Oct 15 '22 23:10

HalR


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.

like image 28
The Vee Avatar answered Oct 15 '22 21:10

The Vee


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.

like image 24
behar Avatar answered Oct 15 '22 22:10

behar


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

like image 43
Tom Taylor Avatar answered Oct 15 '22 23:10

Tom Taylor