Saturday, January 28, 2012

Passing dynamic data from one Activity to another Activity using Bundle in Android applications

One of the basic requirement in any Android application development is the ability to navigate to one Activity from another Activity on an action or event. Also, in most of the cases, we would want to pass data from the source activity to the destination activity.  If the number of parameters that need to be passed from one activity to another is static and is known during compile time, we can use Intent.putExtra(key, value) method to pass the data. But when the parameters are unknown at compile time and are dynamic in nature, we cannot set the values at compile time directly to the Intent. In this case we will use Bundles. Let us discuss how we can achieve this when we develop Android applications using Eclipse.


In our example, we have a LearningActivity (which is the first screen of my application) and a HelloActivity (this would be the activity that I would like to navigate to). The LearningActivity class uses a simple layout that contains an TextEdit and a Button.

User can enter his name in the text box and on click of the button.


When user clicks the button, we want to pass the name that user entered and start HelloActivity which will simply print Hello with the name.


Let us get into action. This is what we are going to do

  1. Attach an onclick listener to the button.
  2. On click of the button, read the entry in the text box
  3. Add the data to a Bundle
  4. Create an Intent with the target activity
  5. Set the bundle as extras to the Intent
  6. Start the next activity

I have added the full source code below. My eclipse project structure is as below


Below is the layout, main.xml, that LearningActivity uses


<?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/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter your name:" />


    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <requestFocus />
    </EditText>


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me" />

</LinearLayout>

Here is the LearningActivity class


package com.test;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class LearningActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Context ctx = this;
        Button b = (Button)findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
EditText et = (EditText)findViewById(R.id.editText1);
String userName = et.getText().toString();

Bundle b = new Bundle();
b.putString("NAME", userName);

Intent t = new Intent(ctx, HelloActivity.class);
t.putExtras(b);
startActivity(t);
}
});
    }
}



Here is hello.xml, which is the layout that HelloActivity uses


<?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:orientation="vertical" >


    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

and here is the code for HelloActivity

package com.test;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HelloActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.hello);
   Bundle b = getIntent().getExtras();
   String userName = (String)b.get("NAME");
   
   TextView t = (TextView)findViewById(R.id.textView1);
   t.setText("Hello "+userName+"!");
}

}



0 comments:

Post a Comment

Blog Archive