1

Possible Duplicate:
How to pass object from one activity to another in Android

I have two activities Activity 1 and Activity 2. For these two activities, I have two XML layout 1.xml and 2.xml. In 1.xml I have a Button and in 2.xml I have TextView. So what I want is on click of Button which is on the first activity I want to open the 2nd activity and also want to display the text shown in Button on the Textview present in Activity2. I mean to say that suppose the text in the Button is "ADD" then this text will be displayed in TextView.

Note: Button is on Activity1 and TextView is on Activity2

iknow
  • 8,358
  • 12
  • 41
  • 68
AndroidDev
  • 4,521
  • 24
  • 78
  • 126

5 Answers5

2

INTENT Use Intent to pass data, putExtra() will allow you to put data

ActivityA

Intent myIntent = new Intent(ActivityA.this, Activityb.class);
myIntent.putExtra("key", "value");
startActivity(myIntent); 

ActivityB

Intent myIntent = getIntent(); // this is just for example purpose
myIntent.getExtra("key");
Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
2

try this using Intent:

In Activity 1 on Button Click:

Intent intent = new Intent(Activityone.class, Activitytwo.class);   
intent.putExtra("value2","world");  
startActivity(intent); 

In Activity 2:

@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    String value1 = super.getIntent().getExtras().getString("value1");  
    myTextView.setText("value1: " + value1 + ");  
}
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
2

First Activity -

Button btn = (Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() 
{
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
                        String passingdata = textview.getText().toString();
                        Intent i = new Intent(Activity1.this, Activity2.class);
                        Bundle b = new Bundle();
                        b.putString("Key", passingdata);
                        i.putExtras(b);
                        startActivity(i);
        }
    });

Second Activity -

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);
    Bundle b = getIntent().getExtras();
    String receivingdata = b.getStringExtra("Key");
    TextView tv = (TextView)findViewById(R.id.secondtext);
    tv.setText(receivingdata);
}
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
1

While starting new activity in your button click write below code

Intent intent = new Intent();
intent.putExtra("TextValue", text1.getText().toString());
intent.setClass(Activity1.this, Activity2.class);
startActivity(intent);

In your Activity2 in onCreate()

String s = getIntent().getStringExtra("TextValue");
Sandip Jadhav
  • 7,377
  • 8
  • 44
  • 76
0

You need to put extra parameters on your intent. Watch this article.

yves.beutler
  • 972
  • 1
  • 14
  • 32