Sunday, October 6, 2013

Android-Passing Data Using an Intent Object

Passing Data Using an Intent Object

Besides returning data from an activity, it is also common to pass data to an activity. For example, in the previous example you may want to set some default text in the EditText view before the activity is displayed. In this case, you can use the Intent object to pass the data to the target activity. The following Try It Out shows you how.

Passing Data Using an Intent Object

Passing Data to the Target Activity
1. Using the same project created in the previous section, add the following statements in bold to the MainActivity.java file:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
{
//startActivity(new Intent(“net.learn2develop.ACTIVITY2”));
//startActivity(new Intent(this, Activity2.class));
/*
startActivityForResult(new Intent(
“net.learn2develop.ACTIVITY2”),
request_Code);
*/
Intent i = new Intent(“net.learn2develop.ACTIVITY2”);
Bundle extras = new Bundle();
extras.putString(“Name”, “Your name here”);
i.putExtras(extras);
startActivityForResult(i, 1);
}
return false;
}

Passing Data Using an Intent Object

2. Add the following statements in bold to Activity2.java:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
String defaultName=””;Bundle extras = getIntent().getExtras();
if (extras!=null)
{
defaultName = extras.getString(“Name”);
}
//---get the EditText view---
EditText txt_username =
(EditText) findViewById(R.id.txt_username); txt_username.setHint(defaultName);
//---get the OK button---
Button btn = (Button) findViewById(R.id.btn_OK);
//---event handler for the OK button---
btn.setOnClickListener(new View.OnClickListener()
{
//...
});
}

Passing Data Using an Intent Object

3. Press F11 to debug the application on the Android Emulator. When you click the center button of the directional keypad, notice that the EditText view in the target activity displays the hint text (see Figure).

Passing Data Using an Intent Object

How It Works
To use the Intent object to carry data to the target activity, you made use of a Bundle object:
Bundle extras = new Bundle();
extras.putString(“Name”, “Your name here”);
i.putExtras(extras);

Passing Data Using an Intent Object

A Bundle object is basically a dictionary object that enables you to set data in key/value pairs. In this case, you created a key named Name and assigned it a value of “Your name here”. The Bundle object is then added to the Intent object using the putExtras() method.
In the target activity, you first use the getIntent() method to obtain the intent that started the activity.
You then use the getExtras() method to obtain the Bundle object:
Bundle extras = getIntent().getExtras();
if (extras!=null)
{
defaultName = extras.getString(“Name”);
}
The getString() method retrieves the Name key from the Bundle object. The string retrieved is then
assigned to the EditText view using the setHint() method:
//---get the EditText view---
EditText txt_username =(EditText) findViewById(R.id.txt_username);
txt_username.setHint(defaultName);


Upload UIImage as base64 String

Upload UIImage as Base64 String (Upload UIImage as string) //Select Pic From Camera or Gallery       @objc func btnPro...