Lecture 5
Lecture 5
Lecture 5
Intent
By
Ts. Dr. Nabilah Radzuan
(Faculty of Computing)
Intent
• Aims
To learn how to passing and receive data between different activities.
• Expected Outcomes
• Ability to handle explicit Intent
• Ability to handle implicit Intent
• Passing data to other activity
• Returning data from launched activity
• References
• http://codetheory.in/
• http://www.techotopia.com/
2
Activities and AndroidManifest.xml
• An Android application can be composed of multiple Activities …
• Each activity should be declared in the file: AndroidManifest.xml
• Add a child element to the <application> tag:
<application>
<activity android:name=".MyActivity" />
<activity android:name=”.SecondActivity" />
</application>
3
Activities and AndroidManifest.xml
• Each activity has its
Java class and
layout file.
public class FirstActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
}
4
Intent
In order to launch another like B Activity from A Activity, programmer
have to use Intent.
5
Intent Definition
6
Intent Definition
• We can think to an “Intent” object as a message containing a
bundle of information.
Information of interests for the receiver (e.g. name)
Information of interests for the Android system (e.g. category).
Component Name
Action Name
Structure
Data
of an Intent
Category
Extra Flags
7
Intent Types
INTENT TYPES
EXPLICIT IMPLICIT
8
Explicit Intent:
• Explicit Intent: Lunching an activity and
passing information from one to
another in the same application.
9
Implicit Intent:
Implicit Intent: In this case programmer can avoid coding his own
activity and let Android automatically handle the activity by firing up
the component from other applications that can do the job on our
app’s behalf.
10
Intents Capability
Programmer will be able to accomplish a lot of things using intent :
1. The ability of navigating from one activity to another Explicit intent
2. The ability to start a third party application (external app’s activity)
like using a camera, contact, pinning location, sending email and
etc.. Implicit intent
11
Intent types: Explicit Intents
Explicit Intent: Specify the name of the Activity that will handle the
intent.
Intent intent=new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
12
Passing Data to Other Activity
Add data to intent in key-value pairs then retrieve it by the receiving
activity.
Intent intent=new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("name","Ali")
intent.putExtra("age",21)
startActivity(intent)
String
Integer
Only a name
In this case, two pieces of data (String and integer) have been transferred by using putExtra.
13
Receiving Data in Other Activity
• To receive the data in B Activity by using Bundle object.
// We are currently in SecondActivity.java
Bundle extras =getIntent().getExtras();
if(extras !=null){
String name = extras.getString("name"); Only a name
Int age = extras.getInt("age")
}
14
Returning Data Launched Activity
• There is no way for transferred data to be returned from the Second
Activity to the first activity.
• However, launching the Second Activity as a sub-activity of the first
Activity can achieved returning the data.
• To do so, we have to start the first activity as a sub-activity by calling
startActivityForResult() method instead of using startActivity().
• This method enable us to identify the returned data from the sub-
activity by passing a REQUEST_CODE value.
startActivityForResult(intent, CHOOSE_ACTIVITY_CODE);
15
Returning Data Launched Activity
Activities can return results (e.g. data)
Sender side: invoke the startActivityForResult()
onActivityResult(int requestCode, int resultCode, Intent data)
startActivityForResult(Intent intent, int requestCode);
16
Intent with Results
• Activities can return results (e.g. data)
• Receiver side: invoke the setResult()
void setResult(int resultCode, Intent data)
• The result is delivered to the caller component only after invoking the
finish() method!
17
Intent with Results
18
Returning Data from Launched Activity
• Returning data to the first activity:
Calling the finish() method in the sub-activity.
finish() method creates a new intent object contains the returned data.
Calling the setResult() method of the enclosing activity to pass through it the
return data.
• The result code is typically
RESULT_OK: the result sent correctly
RESULT_CANCELED: will be received in the parent activity if the event of sub-
activity is crashed
May also be a custom value subject.
19
Java Code Example
Main Activity
Capturing information From the EditText, send it to the second activity,
then do listening
20
Java Code Example
Second Activity
Receiving the information and display it into TextView
21
Java Code Example
Second Activity
Capturing information From the EditText and send it to the main
activity (parent Activity)
22
Intent Types
INTENT TYPES
EXPLICIT IMPLICIT
23
Intent types: Implicit Intents
• Implicit Intents: do not name a target (component name is
left blank) …
• When an Intent is launched, Android checks out which
activies might answer to the Intent …
• If at least one is found, then that activity is started!
• Binding (connections) does not occur at compile time, nor at
install time, but at run-time …(late run-time binding)
24
Intent Components
• We can think to an “Intent” object as a message
containing a bundle of information.
Information of interests for the receiver (e.g. data)
Information of interests for the Android system (e.g. category).
25
Intent Components
• Predefined actions (http://developer.android.com/reference/android/content/Intent.html)
Action Name Description
ACTION_EDIT Display data to edit
ACTION_MAIN Start as a main entry point, does not expect to
receive data.
ACTION_PICK Pick an item from the data, returning what was selected.
ACTION_VIEW Display the data to the user
ACTION_SEARCH Perform a search
26
Intent Components
• Special actions (http://developer.android.com/reference/android/content/Intent.html)
27
Actions
Intent intent=new
Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.ump.edu.my”));
startActivity(intent);
Intent intent=new
Intent(Intent.ACTION_VIEW,
Uri.parse("sms:006011111111"));
startActivity(intent);
Intent intent=new
Intent(Intent.ACTION_DIAL,
Uri.parse("tel:006011111111"));
startActivity(intent);
28
Intent Components
• Example of Implicit Intent that initiates a web search.
public void doSearch(String query) {
Intent intent =new Intent(Intent.ACTION_SEARCH);
Intent.putExtra(SearchManager.QUERY,query);
if (intent.resolveActivity(getPackageManager()) !=null)
startActivity(intent)
}
29
Intent types: Implicit Intents
Intent i = new
Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("www.ump.edu.my"));
startActivity(i);
Data to perform the action on
Action to perform
30
Common Intents
• Setting an Alarm – ACTION_SET_ALARM
• Timer – ACTION_SET_TIMER
• Calendar Events – ACTION_INSERT and data
• Camera – ACTION_IMAGE_CAPTURE /
ACTION_VIDEO_CAPTURE
• Email – ACTION_SEND / ACTION_SENDTO
• Files – ACTION_GET_CONTENT
• Various meanings – ACTION_VIEW
31
3