The intent with Data:
To send data from one Activity to another Activity, we use Intent class object. This includes some set of steps in source activity and some steps in-destination activity.
Steps to send data from Source Activity:
1) Create Intent object by providing source activity and destination activity
Intent i = new Intent(source activity.this, destination activity.class);
2) Attach data to Intent. We use putExtra method of Intent to attach data. This method takes 2 parameters. One is key and the other is value. We use the same keys to retrieve data in destination activity.
To send following values to destination activity,
123
false
c
Naresh IT
i.putExtra(“key1”, 123);
i.putExtra(“key2”, false);
i.putExtra(“key3”, ‘c’);
i.putExtra(“key4”, “Naresh IT”);
3) call startActivity method
startActivity(i);
Altogether,
Intent i = new Intent(SourceActivity.this, DestinationActivity.class);
i.putExtra(“key1”, 123); // Passing int data
i.putExtra(“key2”, false); // Passing boolean data
i.putExtra(“key3”, ‘c’); // Passing char data
i.putExtra(“key4”, “Naresh IT”); // Passing String data
startActivity(i);
Retrieving the data in DestinationActivity:
1) Get the Intent
Intent i = getIntent();
2) Get the data bundle associated to Intent
Bundle b = i.getExtras();
3) Retrieve individual values from Bundle by providing keys
int data1 = b.getInt(“key1”);
boolean data2 = b.getBoolean(“key2”);
char data3 = b.getChar(“key3”);
String data4 = b.getString(“key4”);
Create an android application to describe Intent with Data
XML:
activity_first.xml:
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:gravity=”center”
android:orientation=”vertical”>
<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:onClick=”sendData”
android:text=”Send data” />
</LinearLayout>
activity_second.xml:
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:gravity=”center”
android:orientation=”vertical”>
<TextView
android:id=”@+id/text_result”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:textSize=”25sp”
android:textStyle=”bold” />
</LinearLayout>
Java:
FirstActivity.class
public class FirstActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
}
public void sendData(View v){
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.putExtra(“key1”, 123); // Passing int data
i.putExtra(“key2”, false); // Passing boolean data
i.putExtra(“key3”, ‘c’); // Passing char data
i.putExtra(“key4”, “Naresh IT”); // Passing String data
startActivity(i);
}
}
SecondActivity.class:
public class SecondActivity extends Activity {
TextView textResult;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
textResult = (TextView) findViewById(R.id.text_result);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
int data1 = bundle.getInt(“key1”);
boolean data2 = bundle.getBoolean(“key2”);
char data3 = bundle.getChar(“key3”);
String data4 = bundle.getString(“key4″);
String result = data1 + ” ” + data2 + ” ” + data3 + ” ” + data4;
textResult.setText(result);
}
}
AndroidManifest.xml:
Inside <application> tag,
<activity android:name=”.FirstActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”.SecondActivity” />
HW: Create an android application with one EditText and Button in first activity. On clicking the button, get the data from EditText and send it to second activity show in a TextView.