Bruno Luis Bruno Luis - 3 months ago 14
Java Question

How to refresh previous fragment after closing an activity who was started inside an Adapter?

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??

Answer

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/

Comments