1. What is an intent?
Intent comes from the word “Intention”. In Android, intents are used to communicate asynchronous messages between Android components, either from same application or from other applications.
- An activity on interaction with user can request for another activity.
- An activity of one app can request for taking pictures using inbuilt camera app of your phone.
The Intent class (android.content.intent) in Android is used to display and show our intentions.
We create an object of Intent, passing arguments what we want to do (moving from one activity to another) and pass it to ActivityManager. It will decide when to start the activity.
Intents can be used to start activities, services, broadcast receivers.
2. How to request for another activity from one activity in code?
Intent has a lot of overloaded constructors.
Intent i = new Intent(source, destination)
source ⇒current context (MyActivity.this)
destination ⇒ requested class (NewActivity.class)
Intent i = new Intent(MyActivity.this, NewActivity.class);
Activities which are started by other Android activities are called sub-activities.
3. How to pass messages via intent in code?
An intent can pass data via Bundle.
Example: You want to send the username from one Android activity to next activity.These data can be passed via Bundle by intent.
The reuse of other application components in Android is known as task.
Example: From component of your application, you may need to access Phone Book app of you phone. From your application, you open the PhoneBook app, select one contact, and then return the selected data to your application again.
4. What are the types of intent?
There are two types of intents. These are:-
- Implicit Intent
Calling using Intents:
Uri num = Uri.parse(“tel:”+phNum)
Intent i = new Intent(Intent.ACTION_CALL, num)
Sending sms Intents:
Uri num = Uri.parse(“sms:”+phNum)
Intent i = new Intent(Intent.ACTION_SENDTO, num)
Accessing Web Links
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(“fordoxblog.wordpress.com”));
- Explicit Intent