1. 08. Intent (Explicit)
Oum Saokosal
Master of Engineering in Information Systems, South Korea
855-12-252-752
oum_saokosal@yahoo.com
2. Agenda
• What is Intent?
• Intent: Invoke another Activity explicitly
• Put Extra with Intent
3. What is Intent? (1)
• There are many uses of intent.
– You can use intents to invoke other applications
from your application.
– You can use intents to invoke internal or external
components from your application.
– You can use intents to raise events so that others
can respond in a manner similar to a publish-and
subscribe model.
4. What is Intent? (2)
• At the simplest level, an intent is an action that
you can tell Android to invoke. That is, you can
invoke activity 02 from activity 01.
• In order to invoke like that, you need to register
those activities in AndroidManifest.xml.
• See Example: IntentExplicit.
6. public class Activity01 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main01);
}
public void gotoProfile(View v){
Intent in = new Intent(this, Activity02.class);
startActivity(in);
}
}
8. public class Activity02 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main02);
}
public void backtoMain(View v){
Intent in = new Intent(this, Activity01.class);
startActivity(in);
}
}
11. Put Extra with Intent
• In PHP, you can send data from a file to
another file through session.
• Similar to that, you can send data from one
Activity to other one by putting extra with
Intent.
• The extra data is in the form of key/value
pairs.
12. putExtra() into Intent
public class Activity01 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main01);
}
public void gotoProfile(View v){
Intent in = new Intent(this, Activity02.class);
in.putExtra("id", "4312");
startActivity(in);
}
}
13. getExtras() from getIntent
public class Activity02 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main02);
Bundle extras = getIntent().getExtras();
if (extras != null) {
int id = 0;
if(extras.getString("id") != null){
id = Integer.parseInt(extras.getString("id"));
Toast.makeText(this,
"your id = " + id, Toast.LENGTH_LONG).show();
}
}
}
public void backtoMain(View v){
Intent in = new Intent(this, Activity01.class);
startActivity(in);
}
}