Wednesday, February 8, 2012

Passing values from one Activity to other using Bundle


  • In general Bundle is a bunch of things putting together.
  • Here, in following example I am going to send the data from one activity(SenderActivity) to other activity (ReceiverActivity) through bundle.
  • To start a new activity from existing activity we use Intents (description of an operation to be performed) in Android.
  • In this example  , on button click event I am sending bundle of an employee information from SenderActivity to ReceiverActivity( to display the data sent by SenderActivity).
Step-1: Create a button in main.xml
            <Button         
                        android:layout_width="fill_parent"        
                        android:layout_height="wrap_content"        
                        android:id="@+id/button"        
                        android:text="send bundle data"        
            />
Step-2: Add a listener for the button in SenderActivity.

            Button button=(Button)findViewById(R.id.button);
            button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
        
}
});
  • Bundle of information will be send between activities through Intent objects.So, now we need to create an Intent object.
Step-3: Create an Intent object.
           While creating an Intent object specify  ReceiverActivity as the target  passing it to the Intent's constructor.

           Intent intent=new Intent(SenderActivity.this,ReceiverActivity.class);
Step-4: Create  ReceiverActivity in the package.
            public class  ReceiverActivity extends Activity{
    @Override
             public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.two);
           }
          }
Step-5: Design two.xml layout for  ReceiverActivity to display the bundled information.
  • two.xml contains only TextView's to display the employee details.
        <TextView
        android:id="@+id/showName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="" />
<TextView
        android:id="@+id/showOrg"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="" />
<TextView
        android:id="@+id/showAddr"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="" />
Step-6: Add newly created activity to AndroidManifest.xml
           <activity
                     android:name=". ReceiverActivity " >
            </activity>
Step-7: 
  • Create a bundle object inside the intent and send data to the bundle using putExtras(). And now pass the bundle object to intent.
                        Bundle b=new Bundle();
       b.putString("Name", "GVR");
       b.putString("Organisation", "Kclink");
       b.putString("Address", "Hyderabad");
       intent.putExtras(b);
Step-8:
  • To launch an activity call startActivity() method and pass intent object to it. Then the intent will generate to start  ReceiverActivity .
          startActivity(intent);
Step-9:
  • Now call getIntent.getExtras () from  ReceiverActivity , which will return all extras previously added through putExtras() of the intent that started this activity.
        Bundle bundle=getIntent().getExtras();
        String name=bundle.getString("Name");
        String org=bundle.getString("Organisation");
        String addr=bundle.getString("Address");

Step-10:
  • Finally, set these values to the TextView's to display.
        tv1.setText(name);
        tv2.setText(org);
        tv3.setText(addr);

No comments:

Post a Comment