How to exchange data between Activities in Android – Intent putExtra method
Today’s post is on how to exchange data between activities using Android Intent putExtra and getExtras methods called in an activity code. We are going to show you how to send objects of your own type and read them in an activity. Our example is based on the Android read SMS tutorial. We have modified this application to show SMS contents in a new view. This is the result.
Take a look at the code modifications.
SMSData.java
package com.itcuties.android.apps.data; import java.io.Serializable; /** * This class represents SMS. * * @author itcuties * */ public class SMSData implements Serializable { private static final long serialVersionUID = 3185099950959560099L; // Number from witch the sms was send private String number; // SMS text body private String body; public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } }
We made SMSData class implement Serializable
interface which means that it needs to have a serialVersionUID
class attribute. For more info on Java Serialization see our tutorial.
private static final long serialVersionUID = 3185099950959560099L;
This class objects are now ready to be sent between activities.
activity_sms.xml
This is the view where SMS data is being displayed. There are two TextView
elements here. One for displaying SMS sender number, the second one is for displaying SMS body.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/smsDetailsNumberText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="71dp" android:textColor="#ff0000" android:text="NUMBER_GOES_HERE" /> <TextView android:id="@+id/smsDetailsBodyText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/smsDetailsNumberText" android:layout_centerVertical="true" android:textColor="#00ff00" android:text="BODY GOES HERE" /> </RelativeLayout>
MainActivity.java
This code displays the contents of SMS Inbox in a form of the list using ListView
. This code is explained in details in our previous tutorial – Android read SMS. Our modification for this tutorial was made in the onListItemClick
method.
package com.itcuties.android.apps; import java.util.ArrayList; import java.util.List; import android.app.ListActivity; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.ListView; import com.itcuties.android.apps.data.SMSData; /** * Main Activity. Displays a list of numbers. * * @author itcuties * */ public class MainActivity extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); List<SMSData> smsList = new ArrayList<SMSData>(); Uri uri = Uri.parse("content://sms/inbox"); Cursor c= getContentResolver().query(uri, null, null ,null,null); startManagingCursor(c); // Read the sms data and store it in the list if(c.moveToFirst()) { for(int i=0; i < c.getCount(); i++) { SMSData sms = new SMSData(); sms.setBody(c.getString(c.getColumnIndexOrThrow("body")).toString()); sms.setNumber(c.getString(c.getColumnIndexOrThrow("address")).toString()); smsList.add(sms); c.moveToNext(); } } // Set smsList in the ListAdapter setListAdapter(new ListAdapter(this, smsList)); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { // Get sms data from the list SMSData sms = (SMSData)getListAdapter().getItem(position); // Create an intent Intent intent = new Intent(this, SMSActivity.class); intent.putExtra("sms", sms); // Start sms details activity startActivity(intent); } }
Sending objects between activities – Android putExtra method
This is the code that puts SMSData
object in an intent using putExtra
method and starts a new activity. SMSData
object is available for the new SMSActivity
activity that was just started.
// Create an intent Intent intent = new Intent(this, ActivityClass.class); intent.putExtra("key", yourObjectInstance); // Start activity startActivity(intent);
SMSActivity.java
In this activity SMSData
object contents is being displayed. We are reading data using getIntent().getExtras().get()
method calls.
package com.itcuties.android.apps; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; import com.itcuties.android.apps.data.SMSData; public class SMSActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sms); TextView smsSenderNumber = (TextView)findViewById(R.id.smsDetailsNumberText); TextView smsBody = (TextView)findViewById(R.id.smsDetailsBodyText); // Get sms data SMSData sms = (SMSData)getIntent().getExtras().get("sms"); // Display sms data smsSenderNumber.setText(sms.getNumber()); smsBody.setText(sms.getBody()); } }
Reading object in an activity – Android getExtras() method
To read data that was send to the activity using putExtra method use this code.
YourType yourObjectInstance = (YourType)getIntent().getExtras().get("key");
Download this sample code here.
This code is available on our GitHub repository as well.
Help me, I have a problem with pass data from getExtra Object don’t work. I test your way, but also don’t work.
But ListActivity dont work with ActoinBarActivirty how can fixed it
Hi, I’m totally new at this. I would like some help on understanding the ‘extras’ idea. Is it like passing variable data between two views?
I come from the wonderful land of PHP, where, if you’re familiar with Laravel, or CodeIgniter and such, you can pass data from one view to another.
Is it similar? If so, why is it “Extra” ? Does this bare different significance?
I am grateful to any help.