0

I still don't understand what is so special about Intents. Why not just use a new thread or just call the function? I think I got the whole idea about intents wrong. A simple code showing why Intents are better or when are needed would be great!

Erick Filho
  • 1,962
  • 3
  • 18
  • 31
Ramilol
  • 3,394
  • 11
  • 52
  • 84

4 Answers4

4

Intents are get widely used in android to switch from one activity to other . it is good practice to use intents . Using intents we can pass/send values from one activity to another. So it can be used as value passing mechanism. Also its syntax is very simple.so why to think about threads ?

Rohhit
  • 722
  • 3
  • 12
  • 25
2

Intents are asynchronous messages which allow application components to request functionality from other Android components. Intents allow you to interact with components from the own and other applications. For example an activity can start an external activity for taking a picture.

Intents are objects of the android.content.Intent type. Your code can send them to the Android system defining the components you are targeting. For example via the startActivity() method you can define that the intent should be used to start an activity. An intent can contain data via a Bundle. This data can be used by the receiving component.

To start an activity use the method startActivity(intent). This method is defined on the Context object which Activity extends.

The following code demonstrates how you can start another activity via an intent.

# Start the activity connect to the
# specified class

Intent i = new Intent(this, ActivityTwo.class);
startActivity(i);
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
RAJIL KV
  • 399
  • 1
  • 7
  • 24
1

Why not just use a new thread or just call the function?

No matter what Thread you use, there would still need to be a mechanism to direct the message which is what an Intent does. It is a way to send a message. Now, it needs to be called on the UI Thread or have an appropriate Context passed because it needs this to send the message. Call what function? You are. It calls a constructor of the Intent class.

A simple code showing why Intents are better or when are needed would be great!

I don't have a simple code to compare to because I'm not sure what you want to see. No one is saying that it is better than something else. As to why Intents are used opposed to something else? I don't know...you would have to ask the developers of the Android platform. That is what they decided to use.

When they are needed is when you want to pass a message from one Activity to another or from one application to another. From the docs

An intent is an abstract description of an operation to be performed

I said "message" but the docs say "description of an operation to be performed" (I guess can mean the same thing). You can use them to start an Activity, pass data between Activities, and more such as telling the OS what to do at boot time. Why is it better? Better than what? That is what the developers decided to use so I guess you would have to ask them but maybe they didn't think it was better rather different.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • I understand that you need them to communicate between applications but why use them when you are not communicating with other applications. What excatly do you mean by message? examples of message? – Ramilol Jan 13 '14 at 22:02
  • All `Intents` are sending a message. When starting an `Activity` you are putting in the constructor, "start this class". Or you send a message with an `Intent` saying, "I want to open up the camera". Then when you use it somewhere like in `startActivity` then and only then are you completing an action. – codeMagic Jan 13 '14 at 22:56
0

while I don't have an example, Intents encourage coupling components loosely. It negates the necessity of you building your own Observer design patterns and enables Inter/Intra-Application communication.

rogue-one
  • 11,259
  • 7
  • 53
  • 75