0

I am having the following issue. I have ActivityA and ActivityB. I have some radio buttons in ActivityA and the user can select them. ActivityB is accessed trough ActivityA so when I press a button I go to ActivityB. In ActivityB I do some calculations and I want to return the result to ActivityA but save the changes made in ActivityA before going to ActivityB.

If I use finish() I return to ActivityA and the selected buttons are saved but the result from ActivityB is not passed to ActivityA.

I want to know is there a way to achieve this?

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
user3182266
  • 1,270
  • 4
  • 23
  • 49

1 Answers1

2

Start Activity B using startActivityForResult() and pass the value from Activity B to Activity A using setResult() and retrieve that result from onActivityResult() method.

As example....

In ActivityA.java...

Intent i = new Intent(ActivityA.this, ActivityB.class);    
startActivityForResult(i, 1);

In ActivityB.java...when sent your result back to ActivityA...

Intent i = new Intent();
i.putExtra("result", YourResult);
setResult(1, i);
finish();

Now, in ActivityA, retrieve the result as below...

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) {     
  super.onActivityResult(requestCode, resultCode, data); 

  if(resultCode == 1)
  {
     String result = data.getStringExtra("result");
  } 
}
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41