Darjeeling
Darjeeling

Reputation: 999

use two different startActivityForResult within two different activity

I don't really know how to say it, I'll just describe it with pic

enter image description here

this is how it works:

edittext1 (Enter) -> Listview1 (choose item) -> back to first activity with item from activityA

same with the second one

edittext2 (Enter) -> Listview2 (choose item) -> back to first activity with item from activityB

I tried to use startActivityForResult(set, 0); and startActivityForResult(set, 1); but it didn't work at all

public void onActivityResult(int requestCode,int resultCode, Intent data)
    {
    if(resultCode == 0) {
    //do things for first edittext
    }
    else if(resultCode == 1) {
    //do things for second edittext
    }
}

on activityA and activityB I use this to get their item and bring back to first activity

Intent i = new Intent();
i.putExtra("namaDokter", "kosong");
setResult(RESULT_OK, i);
finish();

Upvotes: 3

Views: 836

Answers (2)

SceLus
SceLus

Reputation: 1107

First you shoud use the @Override anotation above onActivityResult. Second you should check the request code for 0, 1 or whatever code you started the activity. The result code should be compared to Activity.RESULT_OK.

Upvotes: 0

peshkira
peshkira

Reputation: 6229

you have to check the request code, not the result code

the result code is a general code signalling, whether or not the activity finished correctly. The request code is the code that you pass to the new activity in order to differentiate when it finishes.

So basically, you should use something like:

public void onActivityResult(int requestCode,int resultCode, Intent data) {
  if(resultCode == Activity.RESULT_OK) {
   if (requestCode == 0) {
     // do things for first edittext
   } else if (requestCode == 1) {
     //do things for second edit text
   }
  } else {
    // the activity didn't finish with result ok
  }
 }

Upvotes: 2

Related Questions