sriram
sriram

Reputation: 9032

Why do we need an intent to create an activity?

I'm very new to android and I couldn't able to find whats the use of Intent in android.

I can understand the code :

   Intent i = new Intent(getApplicationContext(),Myclass.this);
   startActivity(i);

what it does. But my question is why we need Intent to create an Activity. Why cant android people let us allow to create Activity directly rather than using Intent.

Upvotes: 2

Views: 579

Answers (3)

Trinimon
Trinimon

Reputation: 13967

It's rather an Android design question. The idea behind is, that you always populate only an intent(ion) saying something like "I want to view this URL" or "I want to start my home screen launcher". The system checks what apps are capable to fulfill this request and - if there are several possibilities - allows you to choose one of them.

From a certain perspective this is a bit more flexible. E.g. in my first example above you don't need to know whether the android standard browser is existing or if Chrome has been installed. You simply ask the system to view a URL:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(intent);

So there are some Benefits, though, on the other hand it might appear a bit complicated.

Upvotes: 5

Simon Dorociak
Simon Dorociak

Reputation: 33505

my question is why we need Intent to create an Activity

I would say that it's native build-in feature / mechanism of Android to do some action(for example switch between Activities) and you should follow it.

Generally Intent is "intention" to do some action. You can imagine it as message to say you want to something to happen and you can specify what should happen.

It's strongly question about how is Android OS designed so to make really right and correct answer is quite hard.

Upvotes: 1

Ajay S
Ajay S

Reputation: 48602

Three of the core components of an application — activities, services, and broadcast receivers — are activated through messages, called intents.

Intents are asynchronous messages which allow Android components to request functionality from other components of the Android system. For example an Activity can send an Intents to the Android system which starts another Activity.

Its most significant use is in the launching of activities and start to service, where it can be thought of as the glue between activities.

Intent can be used for open other application also like facebook, twitter, email etc. From here you can email, share photo on facebook and send text on twitter.

Read android developer docs of intent.

Upvotes: 1

Related Questions