Pranit Bankar
Pranit Bankar

Reputation: 473

String not received by passing string between activities

I am sending a String between two Activities. This is my code for the class which is sending the string.

 public class SetData extends Activity implements OnClickListener{

EditText et;
Button bConfirm;
TextView tv;
String bread;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.data_input);
    initiate();
    bConfirm.setOnClickListener(this);
}

private void initiate() {
    // TODO Auto-generated method stub
    et = (EditText) findViewById(R.id.ET);
    bConfirm = (Button) findViewById(R.id.button1);
    tv = (TextView) findViewById(R.id.TV);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    bread = et.getText().toString();
    Bundle basket = new Bundle();
    basket.getString("key", bread);
    Intent stringSend = new Intent(SetData.this, PrintData.class);
    stringSend.putExtras(basket);
    startActivity(stringSend);
} }

This activity is starting another activity as expected. The other activity is the class:

public class PrintData extends Activity implements OnClickListener{

EditText tv;
RadioGroup RG;
Button print;
String newstring = "No content received.";
Bundle gotBasket;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.data_output);
    initiate();
    Intent oldintent = getIntent();
    gotBasket = oldintent.getExtras();
    newstring = gotBasket.getString("key");
    tv.setText(newstring);
    //print.setOnClickListener(this);
}

private void initiate() {
    // TODO Auto-generated method stub
    tv = (EditText) findViewById(R.id.TV);
    print = (Button) findViewById(R.id.print);
    RG = (RadioGroup) findViewById(R.id.radioGroup);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

} }

The default value of String newstring is "No content received.". When the new Activity opens, it shows the above text. Which means the string is sent by Activity1 (SetData), but not received by Activity2 (PrintData).

What could be the reason for this ? How can I correct it ?

Upvotes: 0

Views: 50

Answers (3)

Amit kumar
Amit kumar

Reputation: 1605

In your first activity your doing basket.putString("key", bread);

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    bread = et.getText().toString();
    Bundle basket = new Bundle();
    basket.putString("key", bread);
    Intent stringSend = new Intent(SetData.this, PrintData.class);
    stringSend.putExtras(basket);
    startActivity(stringSend);
} 

or first Activity:

stringSend.putExtra("key", bread)

Secend Activity: get the value

newstring=getIntent().getExtras().getString("key");

Upvotes: 0

eLemEnt
eLemEnt

Reputation: 1801

In your first activity your doing basket.getString("key", bread); to send data using bundle you should use basket.putString("key", bread);

Upvotes: 1

vinitius
vinitius

Reputation: 3274

You should do basket.putString() in your first activity, not getString()

Upvotes: 2

Related Questions