Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onActivityResult return data from fragment to activity

I Have an activity A that opens an activity B using startActivityForResult.

Now in activity B it's an activity fragment holder as well it contains an ActionBar with menu items.

Now whenever I press action bar button in activity B it should return data from selected fragment of an activity B not to its holder instead it should return data to activity A because it's the one who did the launch.

So it's basically passing data fragment (inside activity B) to activity B then to Activity A.

I am trying hopelessly to find a way to solve it. Is there any possible way to do it?

like image 295
mba3gar Avatar asked Oct 27 '25 03:10

mba3gar


2 Answers

Disclaimer there are many ways, this is the one I prefer, not the best ever and not the perfect one, I just like this.

The easiest way, in my opinion, is to pass the data from Fragment inside B to ActivityB, then from ActivityB to ActivityA.

Step 1 to pass data from Fragment to container activity you have many ways; the one I usually use is to use an Interface:

Create interface for ActivityB

public interface IActivityB {
    void setDataAAndFinish(whateverType data);
}

Implement interface in your activityB

public class InterventoActivity extends AppCompatActivity implements IInterventoActivity {

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    private Bundle dataA = null;
    @Override
    public void setDataAAndFinish(whateverType data) {
        dataA = data;
        Intent intent = new Intent();
        intent.putExtra("data", data)
        setResult(RESULT_OK, intent);        
        finish();
    }
}

Set activityA to request and accept return from ActivityB

first, start activityB for result and not normally

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

Then read result

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1) {
         if(resultCode == RESULT_OK) {
             whateverType data = data.getStringExtra("data");
         }     
    }
} 

Now from fragment

((IActivityB)getActivity()).setDataAAndFinish(myDatas);
like image 169
Pier Giorgio Misley Avatar answered Oct 28 '25 17:10

Pier Giorgio Misley


You need to declare a function in your ActivityB like the following.

public void sendDataBackToActivityA(String dataToBePassedToActivityA) {
    Intent intent = new Intent();
    intent.putExtra("data", dataToBePassedToActivityA);
    setResult(RESULT_OK, intent);
    finish();
}

Now from the Fragment that you launched from ActivityB, just call the method on some action in your Fragment that was launched from ActivityB. So the pseudo implementation of the process in your Fragment should look like the following. Declare this function in your Fragment and invoke the function on some action in your Fragment.

public void sendDataToActivityAFromFragment(String dataToBePassed) {
    ((ActivityB)getActivity()).sendDataBackToActivityA(dataToBePassed);
}

This will serve your purpose I hope.

like image 31
Reaz Murshed Avatar answered Oct 28 '25 15:10

Reaz Murshed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!