Android - passing data between Activities

Posted by Bill Osuch on Geeks with Blogs See other posts from Geeks with Blogs or by Bill Osuch
Published on Mon, 17 Jan 2011 21:53:56 GMT Indexed on 2011/01/17 22:54 UTC
Read the original article Hit count: 466

Filed under:

(To follow along with this, you should understand the basics of starting new activities: Link )

The easiest way to pass data from one activity to another is to create your own custom bundle and pass it to your new class.

First, create two new activities called Search and SearchResults (make sure you add the second one you create to the AndroidManifest.xml file!), and create xml layout files for each. Search's file should look like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <TextView 
        android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="Name:"/>
    <EditText       
        android:id="@+id/edittext"       
 android:layout_width="fill_parent"       
 android:layout_height="wrap_content"/>
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="ID Number:"/>
    <EditText       
        android:id="@+id/edittext2"       
        android:layout_width="fill_parent"       
        android:layout_height="wrap_content"/>
    <Button  
        android:id="@+id/btnSearch" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Search" />
</LinearLayout>

and SearchResult's should look like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <TextView 
        android:id="@+id/txtName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <TextView 
        android:id="@+id/txtState"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="No data"/>
</LinearLayout>

Next, we'll override the OnCreate method of Search:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);

    Button search = (Button) findViewById(R.id.btnSearch);
    search.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
             
            Intent intent = new Intent(Search.this, SearchResults.class); 
            Bundle b = new Bundle();
             
            EditText txt1 = (EditText) findViewById(R.id.edittext);
            EditText txt2 = (EditText) findViewById(R.id.edittext2);
                        
            b.putString("name", txt1.getText().toString());
            b.putInt("state", Integer.parseInt(txt2.getText().toString())); 
               
            //Add the set of extended data to the intent and start it
            intent.putExtras(b);
            startActivity(intent); 
        }

    });
}

This is very similar to the previous example, except here we're creating our own bundle, adding some key/value pairs to it, and adding it to the intent.

Now, to retrieve the data, we just need to grab the Bundle that was passed to the new Activity and extract our values from it:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search_results);

    Bundle b = getIntent().getExtras();
    int value = b.getInt("state", 0);
    String name = b.getString("name");
       
    TextView vw1 = (TextView) findViewById(R.id.txtName);
    TextView vw2 = (TextView) findViewById(R.id.txtState);
       
    vw1.setText("Name: " + name);
    vw2.setText("State: " + String.valueOf(value));
}

 


 

© Geeks with Blogs or respective owner