0

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. enter image description here. 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.

Community
  • 1
  • 1
Illegal Argument
  • 10,090
  • 2
  • 44
  • 61
  • look this one : http://stackoverflow.com/questions/24321449/android-navigation-drawer-unable-to-settext-in-a-fragment-from-mainactivity/24322359#24322359 – Umang Kothari Jun 23 '14 at 12:34
  • @Umang sorry pal that doesnot help – Illegal Argument Jun 23 '14 at 12:35
  • 1
    So basically you have a parent Fragment with multiple child Fragments which using a dialog. And the dialog should inform the parent Fragment about an increase of the comments? – Steve Benett Jun 23 '14 at 12:55
  • @SteveBenett you are almost right the child fragment needs to be informed from the dialog. I passed a textview reference to the newInstance() method as a quickfix for this problem but I am looking for the correct way to do this. – Illegal Argument Jun 23 '14 at 13:35
  • Why no communication between dialog and the fragment through the interface? The way from dialog -> Activity -> fragment isn't necessary here, you can go the direct way. – Steve Benett Jun 23 '14 at 14:04
  • @SteveBenett how can I do that? My normal flow would be Dialog->Activity containing all fragment->the root fragment Activity ->the child fragment with textview I want to update. I tried setTargetFragment() and getTargetFragment() but It is unable to solve my problem – Illegal Argument Jun 23 '14 at 14:51

0 Answers0