startActivityForResult from an adapter and receive the data in the fragment

0

I have the following:

  • MainActivity
  • Fragment1 with recylerview
  • SimpleAdapter

Des of the RecyclerView.Adapter when doing click send a Intent with startActivityForResult

((Activity) mContext).startActivityForResult(item.getIntent().addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), 0x600);

To capture the result I do it at MainActivity

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.d(TAG, "onActivityResult() called with: requestCode = [" + requestCode + "], resultCode = [" + resultCode + "], data = [" + data + "]");
}

Here everything is correct, I receive the result perfectly, but I would like the result to be computed in Fragmento1 to make changes in RecyclerView .

I see that in Fragment you can use onActivityResult but it does not capture anything.

Is there any way to delegate the onActivityResult of the activity to the fragment?

    
asked by Webserveis 06.03.2018 в 13:34
source

1 answer

0

My solution To delegate the result to all the fragments that are operating.

From MainActivity

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.d(TAG, "onActivityResult() called with: requestCode = [" + requestCode + "], resultCode = [" + resultCode + "], data = [" + data + "]");
    for (Fragment fragment : getSupportFragmentManager().getFragments()) {
        fragment.onActivityResult(requestCode, resultCode, data);
    }

In the Fragment, it is received in the same way.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        //@todo computar el resultado
    }
}
    
answered by 06.03.2018 / 14:52
source