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.