Developing the live feed of the event page from Facebook for the Open Event Android App, there were questions how best to display the comments in the feed. A dialog fragment over the feeds on the click of a button was the most suitable solution. Now the problem was, a dialogfragment can only be called from an app component (eg- fragment or an activity). Therefore, the only challenge which remained was to call the dialogfragment from the adapter over the feed fragment with the corresponding comments of the particular post on a button click.
What is a dialogfragment?
A dialogfragment displays a dialog window, floating on top of its activity’s window. This fragment contains a Dialog object, which it displays as appropriate based on the fragment’s state. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog (Developer.Android.com).
The solution which worked on was to define a adapter callback interface with a onMethodCallback method in the feed adapter class itself with the list of comment items fetched at runtime on the button click of a particular post. The interface had to be implemented by the main activity which housed the feed fragment that would be creating the comments dialogfragment with the passed list of comments.
Define an interface adapterCallback with the method onMethodCallback parameterized by the list of comment items in your adapter class.
Create a constructor of the adapter with the adapterCallback as a parameter. Do not forget to surround it with a try/catch.
On the click of the comments button, call onMethodCallback method with the corresponding comment items of a particular feed.
Finally implement the interface in the activity to display the comments dialog fragment populated with the corresponding comments of a feed post. Pass the comments with the help of arraylist through the bundle.
The comments generated with each feed post in the open event android app does complement the feed well. The pagination is something which is an option in the comments and the feed both however that is something for some other time. Until then, keep coding!
- StackOverFlow Answer as a reference https://stackoverflow.com/questions/13733304/callback-to-a-fragment-from-a-dialogfragment
- Complete code reference https://github.com/fossasia/open-event-android/pull/1710/files
- Dialog Fragment Android Official Documentation https://developer.android.com/reference/android/app/DialogFragment.html