0

In my project, ActivityTwo is launched as a sub-activity of ActivityOne, which has, in turn, been modified to handle data returned from ActivityTwo.

TextView in ActivityOne should retrieve the text entered into the EditText added to intent object as a key-value in ActivityTwo. Despite of the successful launch, it does not extract the intent object.

I looked through the possible similar problems faced and came through this: How to manage `startActivityForResult` on Android?

Yet, I still could not figure out why my TextView cannot set the text extracted from intent object retrieved from the second activity.

Any useful help appreciated.

Thanks in advance Below you can see the codes:

ActivityOne:

public class ActivityOne extends AppCompatActivity {
    private static final int REQUEST_CODE = 1;
    EditText isimAlani, soyadiAlani;
    TextView tlfNo;
    Button btnIleri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);

        isimAlani = (EditText) findViewById(R.id.isimalani);
        soyadiAlani = (EditText) findViewById(R.id.soyadialani);
        tlfNo = (TextView) findViewById(R.id.tlfno);
        btnIleri = (Button) findViewById(R.id.btnileri);

        btnIleri.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(ActivityOne.this, ActivityTwo.class);
                i.putExtra("isim", isimAlani.getText().toString());
                i.putExtra("soyisim", soyadiAlani.getText().toString());
                startActivityForResult(i, REQUEST_CODE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);        
        if ((requestCode == REQUEST_CODE) && (requestCode == RESULT_OK)) {
            String result = data.getExtras().getString("returnData");
            tlfNo.setText("Telefon numaranız: " + result);
        }
    }
}

ActivityTwo:

public class ActivityTwo extends AppCompatActivity {
    EditText tlfNoAlani;
    TextView tvAdSoyad;
    Button btnGeri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);

        tlfNoAlani = (EditText) findViewById(R.id.tlfnoalani);
        tvAdSoyad = (TextView) findViewById(R.id.tvadsoyad);
        btnGeri = (Button) findViewById(R.id.btngeri);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            tvAdSoyad.setText("Merhaba " + extras.getString("isim") + " " + extras.getString("soyisim"));
        }

        btnGeri.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent data = new Intent();
                String returnString = tlfNoAlani.getText().toString();
                data.putExtra("returnData", returnString);
                setResult(RESULT_OK, data);
                finish();
            }
        });
    }
}
Community
  • 1
  • 1
Gulbala Salamov
  • 198
  • 3
  • 9

2 Answers2

1

Could it be that there's a typo in your ActivityOne.onActivityResult() code?

It looks like you check if requestCode equals two possibly different values REQUEST_CODE and RESULT_OK.

1

As Pavel suggested the mistake was in onActivityResult() method.

So I did the following changes in ActivityOne source file.

Simply I replaced one of the requestCode with resultCode and now it does work!

Thanks him again

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if ((requestCode == REQUEST_CODE) && (resultCode == RESULT_OK)) {
        String result = data.getExtras().getString("returnData");
        tlfNo.setText("Telefon numaranız: " + result);
    }
}
Gulbala Salamov
  • 198
  • 3
  • 9