Passing Activity A's data into Activity B

Posted by user1058153 on Stack Overflow See other posts from Stack Overflow or by user1058153
Published on 2011-11-26T08:51:09Z Indexed on 2011/11/26 9:52 UTC
Read the original article Hit count: 273

Filed under:

What i am trying to show here is that I am trying to pass the data in Activity A to Activity B. Activity A mainly there are 3 textbox for me to key in something then a button to go to Activity B(Confirmation Page) and in Activity B, i am able to show what i have keyed in Activity A. I am new to Android, so can someone guide me through this?

In Activity A

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

    Textview01 = (EditText) this.findViewById(R.id.txtView1);
    Textview02 = (EditText) this.findViewById(R.id.txtView2);
    Textview03 = (EditText) this.findViewById(R.id.txtView3);

    mButton = (Button) findViewById(R.id.button);
    mButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            Intent i = new Intent(ActivityA.this, ActivityB.class); 
            i.putExtra("Textview01", txtView1.getText().toString());
            i.putExtra("Textview02", txtView2.getText().toString());
            i.putExtra("Textview03", txtView3.getText().toString());
            startActivity(i);

In Activity B.

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

    TextView txtPickup = (TextView) this.findViewById(R.id.txtPickup);
    TextView txtLocation = (TextView) this.findViewById(R.id.txtLocation);
    TextView txtDestination = (TextView) this.findViewById(R.id.txtDestination);

    txtLocation.setText(getIntent().getStringExtra("Location"));
    txtPickup.setText(getIntent().getStringExtra("Pick Up Point"));
    txtDestination.setText(getIntent().getStringExtra("Destination"));

In my Activity B XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="txtView01:" />
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/txtView01"></TextView>

    <TextView android:id="@+id/TextView02" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="txtView02:"></TextView>
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/txtView02"></TextView>

    <TextView android:id="@+id/TextView02" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="txtView03:"></TextView>
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/txtView03"></TextView>

    <Button android:id="@+id/btnButton" android:layout_height="wrap_content"
        android:layout_width="fill_parent" android:text="Book now" />

</LinearLayout>

Can someone tell me if this is correct? I'm getting some error like a popup Instrumental.class.

LogCat shows :

11-26 17:27:40.895: INFO/ActivityManager(52): Starting activity: Intent { cmp=ActivityA/.ActivityB (has extras) }
    11-26 17:27:42.956: DEBUG/dalvikvm(252): GC_EXPLICIT freed 156 objects / 11384 bytes in 346ms
    11-26 17:27:47.815: DEBUG/dalvikvm(288): GC_EXPLICIT freed 31 objects / 1496 bytes in 161ms

© Stack Overflow or respective owner

Related posts about android