7

I have a fragment (FragmentA) with a RecyclerView displaying some CardViews. I wanted to make the cardviews clickable , so the solution I found for this was to implement View.OnClickListener in the ViewHolder inside de adapter class. Something like this:

fragmentAAdapter.java

public class fragmentAViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {       

    public fragmentAViewHolder (View itemView) {
        super(itemView);            

        itemView.setOnClickListener(this);
    }     

    @Override
    public void onClick(View v) {
        //do something..
    }
}

The onClick() method gets the data from the clicked cardview and start an activity (InfoActivity) who displays the data.

OnClick() method

@Override
    public void onClick(View v) {            
        Intent intent = new Intent(itemView.getContext(), InfoActivity.class);

        Bundle mBundle = new Bundle();
        mBundle.putParcelable("someData", someData);
        intent.putExtras(mBundle);            
        itemView.getContext().startActivity(intent);
    }

In the InfoActivity I can update and delete the data. After that I want to finish the InfoActivity and refresh the FragmentA to get the new data.

To close the activity and refresh the previous fragment I am trying to use the startActivityForResult(). Like this:

@Override
    public void onClick(View v) {            
        Intent intent = new Intent(itemView.getContext(), InfoActivity.class);       

        ((Activity)  itemView.getContext()).startActivityForResult(intent, 10001);

    }

InfoActivity.java

@Override
    public void onClick(DialogInterface dialog, int which) {        
    setResult(InfoActivity.RESULT_OK);
    finish();
}

FragmentA.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if ((requestCode == 10001) && (resultCode == InfoActivity.RESULT_OK)) {

        FragmentAfragment  = new FragmentA();
        android.support.v4.app.FragmentTransaction fragmentTransaction =
                getFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();
    }
}

But when I close the InfoActivity nothing happens.

The problem is that the setResult(InfoActivity.RESULT_OK) returns the result to the adapter class (fragmentAAdapter) not to FragmentA, so the method onActivityResult() in FragmentA is never called.

How do I refresh the FragmentA after close InfoActivity who was started inside the onClick() method in the fragmentAAdapter??


Update - Solution

I overwrote the method onActivityResult() in the activity of the FragmentA. Like this:

MainActivity.java

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

    super.onActivityResult(requestCode, resultCode, data);
    if ((requestCode == 10001) && (resultCode == 10001)) {

        FragmentA fragment  = new FragmentA();
        android.support.v4.app.FragmentTransaction fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();
    }
}

and in InfoActivity.java:

@Override
public void onClick(DialogInterface dialog, int which) {        
    setResult(10001);
    finish();
}

Every time it closes an activity and go back to the FragmentA the onActivityResult() in the MainActivity is called, and when the condition if((requestCode == 10001) && (resultCode == 10001)) is true the FragmentA is refreshed.

Bruno Santos
  • 135
  • 1
  • 10
  • You can easily verify your suspicions in the debug mode. Simply put a breakpoint in `onActivityResult()` and see if it hits. If not, we'll know for sure that's the issue. – Vucko Aug 18 '16 at 19:43
  • @Vucko Yeah, this is the problem. I did what you said and the onActivityResult() is not even called. – Bruno Santos Aug 18 '16 at 19:51
  • If all else fails, I can provide an alternative solution for communication between different components - [EventBus](https://github.com/greenrobot/EventBus). – Vucko Aug 18 '16 at 20:05

2 Answers2

6

In the activity of your fragment you can do something like this:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    requestCode &= 0xffff;
    for (Fragment fragment : getSupportFragmentManager().getFragments()) {
        if (fragment != null) {
            fragment.onActivityResult(requestCode, resultCode, data);
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}

Or you can use a Event bus like: http://square.github.io/otto/

  • Thanks for your answer. Overwriting onActivityResult() in the activity of the fragment helped me solving my problem. I'll update my question with the solution I used. – Bruno Santos Aug 18 '16 at 20:54
0

The problem you are issuing is the things like that

Fragment(startActivityForResult()) -> Activity

Fragment(onActivityResult()) <- Activity (setResult(Result_OK))

You should avoid to do the onActivityResult() in your activity class since the role of the activity in this process is just a mailer to send the data back to the fragment.

If you are going to call the activity inside the fragment you can call it getActivity().startActivityForResult() directly

Fragment(getActivity().startActivityForResult) -> Activity

Activity(onActivityResult()) <- Activity (setResult(Result_OK))

#Options

Activity(Loop the fragment and find Fragment and call onActivityResult() like Agustin Sivoplas suggested.) -> Fragment

Even though you are inside the adapter view holder, it should be also located inside the fragment.Try to utilize the fragment function and not make extra code in Activity

Possible duplicate

onActivityResult is not being called in Fragment

Community
  • 1
  • 1
Long Ranger
  • 5,888
  • 8
  • 43
  • 72