I am facing a problem implementing an interface defined in one fragment and using it to another. I know I need to do this through activity but I have added fragments dynamically inside another fragment. Please look at the snapshot to understand more about my problem.
. I have a fragment called ACTIVITY fragment inside which I load fragments dynamically. The Comments textview is clickable and when clicked it a CommentDialogFragment is shown. This dialog fragment is shared by all the fragments. Code when comment clicked:
FragmentTransaction ft = getActivity()
.getSupportFragmentManager().beginTransaction();
CommentDialog fragment = CommentDialog.newInstance(id,
"Activity");
ft.addToBackStack(null);
fragment.show(ft, null);
I want to increment the comment count. For that I made an interface:
public interface IncrementComment {
public void increaseCommentCount(boolean increase);
}
I am unable to use this interface in my fragment inside the ACTIVITY fragment. The interface is detected on the Activity that holds all these fragments. Heres the interface in my MainActivity class I get the data upto this point:
@Override
public void increaseCommentCount(boolean increase) {
// TODO Auto-generated method stub
Log.d("interface main activity", "called");//this is called
}
Now I am unable to pass data from this activity code to my fragment because the fragments are loaded dynamically and there can be any number of fragments(user can see their old post). So I tried to make a fragment implement the interface to update the value of the textview. But I couldnot get it working as it is never called. Can someone point me in the correct direction. I tried most of the links in SO like this and from other sources like this but none of them fit my requirement.