-4

Hi I am getting the problem to send the data from second activity to first activity

Intent i=new Intent();
i.setClass(SecondActivity.this,MainActivity.class);
Toast.makeText(SecondActivity.this, "this is second activity", Toast.LENGTH_SHORT).show();
String name=ed1.getText().toString();
i.putExtra("ok",name);
setResult(RESULT_OK, i);
startActivity(i);

how can we recive the data on first activity

MBH
  • 16,271
  • 19
  • 99
  • 149
anand kumar
  • 121
  • 1
  • 3
  • 9

2 Answers2

0

Here is working example for getting data from second activity.

//First activity
private static final int PLAY_GAME = 1010;

@Override
protected void onActivityResult(int requestCode,
                                int resultCode, Intent data) {
    if (requestCode == PLAY_GAME && resultCode == RESULT_OK) {

        String getData = data.getExtras().getString("returnStr");

    }
    super.onActivityResult(requestCode, resultCode, data);
}





//second activity
Intent i = getIntent();
i.putExtra("returnStr", data);
    setResult(RESULT_OK,i);
    finish();
Kashif Anwaar
  • 728
  • 7
  • 14
0

first, define a variable in you first activity like this (100 is just random, pick whatever you want):

private static final int SECOND_ACTIVITY = 100;

then in your first activity you start the second activity like this:

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivityForResult(intent, SECOND_ACTIVITY);

also override onActivityResult in your first activity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == SECOND_ACTIVITY) {
        if (resultCode == RESULT_OK) {
            String foo = data.getStringExtra("foo");
        }
    }
}

when you finish your second activity put you data, like this:

Intent data = new Intent();
data.putExtra("foo", "bar");
setResult(RESULT_OK, data);
finish();
Shalev Moyal
  • 644
  • 5
  • 12