You are on page 1of 25

Android Intent

 Renaldi Primaswara P., M.Kom


Contents
 Intent
 Explicit and implicit Intents
 Starting Activities
 Sending and Receiving Data
 Starting Activity for Result
Intent
What is an intent?
 An intent is a description Originator App component
of an operation to be
performed. Intent Action

 An Intent is an object
Android System
used to request an
action from another
application component
via the Android system.
What can intents do?
 Start activities
 A button click starts a new activity for text entry
 Clicking Share opens an app that allows you to post a
photo
 Start services
 Initiate downloading a file in the background

 Deliver broadcasts
 The system informs everybody that the phone is now
charging
Explicit and implicit intents
Explicit Intent
 Starts a specific activity
 Main activity starts the ViewShoppingCart activity

Implicit Intent
 Asks system to find an activity that can handle
this request
 Clicking Share opens a chooser with a list of apps
Start an Activity with an
Explicit Intent
 To start a specific activity, use an explicit intent
 Create an intent
Intent intent =
new Intent(this, ActivityName.class);
 Use the intent to start the activity
startActivity(intent);
Starting
Activities
How Activities Run
 All Activities are managed by the Android runtime
 Started by an "intent", a message to the Android
runtime to run an activity

User clicks MainActivity FoodListActivity OrderActivity


launcher icon What do you want to do? Choose food items...Next Place order

Intent:
Start app Android Android Android
System Start main Intent: System Start Intent: System Start finish
activity Shop choose order order activity
food
activity
Intents
Starting New Activity

// create Intent containing Activity class


Intent intent =
new Intent( Context, Activity.class);

// start Activity from Intent


startActivity(intent);

Note: don't forget to add started Activity into AndroidManifest.xml


Intent: Launching an
Activity
 Launching an activity

Intent intent =
new Intent (this, HelloWorld.class);
startActivity(intent);
Sending and
Receiving Data
Two types of sending data
with Intents

 Data—(intent.setData()) one piece of


information whose data location can be
represented by an URI(Uniform Resource Identifier)
 Uri contoh : http://www.example.com/, ftp://example.com, tel:+44123456789

 Extras—(intent.putExtra())one or more
pieces of information as a collection of key-value
pairs in a Bundle (berkas)
 Limited to primitives data types
Sending and retrieving data
In the first (sending) activity In the second (receiving) activity

 Create the Intent object  Get the intent object the


activity was started with
 Put data or extras into that
intent  Retrieve the data or extras
from the Intent object
 Start the new activity with
startActivity()
Putting a URI as intent data
A web page URL
intent.setData(
Uri.parse("http://www.google.com"));

A file URI
intent.setData(
Uri.fromFile(new File("/sdcard/sample.jpg")));
Intent: Launching an
Activity
 Launching an activity with extras (data)
Intent intent =
new Intent (this, HelloWorld.class);
intent.putExtra("title", "Hello World App
Title");
intent.putExtra("x", 2);
startActivity(intent);
Intent: Launching an
Activity
 If lots of data, create a bundle and pass the
bundle.
Bundle bundle = new Bundle();
bundle.putInt("num", 3);
bundle.putBoolean("success", true);
bundle.putFloat("score", 2.3f);
bundle.putString("name", "DJ-Oko");
intent.putExtras(bundle);
startActivity(intent);
Get data from intents
 getData()
Uri locationUri = intent.getData();
 int getIntExtra (String name, int defaultValue)
int level = intent.getIntExtra("level", 0);
 Get all the data at once as a bundle.
Bundle bundle = intent.getExtras();
Starting Activity
For Result
Returning data to the
starting activity
1. Implement onActivityResult() in first activity
2. Use startActivityForResult() to start the
second/destination activity
3. To return data from the second Activity:
 Create a new Intent or getIntent which start that activity
 Put the response data in the Intent using putExtra()
 Set the result to Activity.RESULT_OK
or RESULT_CANCELED, if the user cancelled out
 call finish() to close the activity, pop the backstack and return to
the previous activity
Starting Activity For Result
 Create an Intent that start other Activity

// set any unique request code


int reqCode = 99;
Intent intent = new Intent(this, ActivityTujuan.class);

// put some data


intent.putExtra("jordan", 23);

this.startActivityForResult(intent, reqCode);
Processing Data Sent from
Activity1 in Activity2
 Getting value sent from Intent data

Intent intent = getIntent();


int number = intent.getIntExtra("jordan", 0);
// do something with the value

Default value given


if no specified data
with specified key is sent
Returning Result back to the
Calling Activity
 Returning result as Intent

// create result data as Intent


Intent resultIntent = new Intent(); //atau getIntent()
resultIntent.putExtra("result", "Slam Dunk!");

// set result status


setResult(RESULT_OK, resultIntent);

// end current Activity


// and return to calling Activity
finish();
Receive the Result in
Activity1
 Receive the result from other Activity
@Override
protected void onActivityResult(
int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == reqCode) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
String text = data.getStringExtra("result");
// do something with resulting data
}
}
}
Questions?
Let’s Code

You might also like