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
  • 152,872
  • 27
  • 286
  • 297
user3182266
  • 1,142
  • 4
  • 21
  • 47

3 Answers3

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,503
  • 4
  • 29
  • 40
1

From your FirstActivity call the SecondActivity using startActivityForResult() method

eg:

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

In your SecondActivity set the data which you want to return back to FirstActivity, If you don't want to return back don't set any.

eg: In secondActivity if you want to send back data:

 Intent returnIntent = new Intent();
 returnIntent.putExtra("result",result);
 setResult(RESULT_OK,returnIntent);     
 finish();

if you don't want to return data:

Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);        
finish();

Now in your FirstActivity class write following code for onActivityResult() method

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

  if (requestCode == 1) {

     if(resultCode == RESULT_OK){      
         String result=data.getStringExtra("result");          
     }
     if (resultCode == RESULT_CANCELED) {    
         //Write your code if there's no result
     }
  }
}//onActivityResult
Boris Knez
  • 84
  • 2
0

Yes.. you can use StartActivityForResult:

From your FirstActivity call the SecondActivity using startActivityForResult() method

Example:

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

In your SecondActivity set the data which you want to return back to FirstActivity, If you don't want to return back don't set any.

eg: In secondActivity if you want to send back data:

Intent returnIntent = new Intent();
 returnIntent.putExtra("result",result);
 setResult(RESULT_OK,returnIntent);     
 finish();

if you don't want to return data:

Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);        
finish();

Now in your FirstActivity class write following code for onActivityResult() method

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

  if (requestCode == 1) {

     if(resultCode == RESULT_OK){      
         String result=data.getStringExtra("result");          
     }
     if (resultCode == RESULT_CANCELED) {    
         //Write your code if there's no result
     }
  }
}

Refere

One more tutorial

Community
  • 1
  • 1
Lokesh
  • 5,258
  • 4
  • 26
  • 43