Geeks With Blogs
Bill Osuch - Random geek notes

(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));
}

Update (3/13/2012): You can download a complete Eclipse project at this link: PassingDataBlogPost.zip

Technorati Tags:

Posted on Monday, January 17, 2011 3:53 PM Android | Back to top


Comments on this post: Android - passing data between Activities

# re: Android - passing data between Activities
Requesting Gravatar...
Thanks !
It is nicely described
Left by Subrat on Feb 16, 2011 3:35 PM

# re: Android - passing data between Activities
Requesting Gravatar...
thanks man! so easy to understand
Left by ajay on Aug 06, 2011 1:06 PM

# re: Android - passing data between Activities
Requesting Gravatar...
Thanks dude
Left by Himanshu on Sep 04, 2011 3:38 AM

# re: Android - passing data between Activities
Requesting Gravatar...
your example program very useful for my beginning android application ,thanks man!
Left by karthikeyan on Dec 29, 2011 3:59 AM

# re: Android - passing data between Activities
Requesting Gravatar...
Nice guide, I am getting a force close when I hit search though :(
Left by Rob on Dec 29, 2011 3:51 PM

# re: Android - passing data between Activities
Requesting Gravatar...
My application is forced to close!!

I'm following your guides exactly :(

Do we have to add special thing in the Manifest ??
Left by Rose on Jan 12, 2012 7:43 AM

# re: Android - passing data between Activities
Requesting Gravatar...
Your attempt is very good but when empty search it should ask some requirement know... write that one also..
Left by Nagesh on May 14, 2012 5:00 AM

# re: Android - passing data between Activities
Requesting Gravatar...
thanks a lot geek!!
it helped me a lot!!
Left by kumar on Jun 01, 2012 1:18 PM

# re: Android - passing data between Activities
Requesting Gravatar...
Thanks a lot man!!! It was very helpful!!!
Left by dan on Nov 19, 2012 11:52 PM

# re: Android - passing data between Activities
Requesting Gravatar...
God bless you that was so easy
Left by Mesonrale Ope on May 03, 2013 2:57 AM

# re: Android - passing data between Activities
Requesting Gravatar...
Perfect! Thanks for the tutorial.
Left by lifeishot on Jul 22, 2015 11:27 PM

Your comment:
 (will show your gravatar)


Copyright © Bill Osuch | Powered by: GeeksWithBlogs.net