Professional Documents
Culture Documents
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
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
Intent intent =
new Intent (this, HelloWorld.class);
startActivity(intent);
Sending and
Receiving Data
Two types of sending data
with Intents
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
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
this.startActivityForResult(intent, reqCode);
Processing Data Sent from
Activity1 in Activity2
Getting value sent from Intent data