java - How to handle multiple fragment interaction listeners in one Activity properly? -
i have 1 activity
, 6 different fragments
attached it. each fragment has onfragmentinteractionlistener
interface , activity implements these listeners in order receive callbacks. looks little messy, i'm interested there patterns/ways simplify , make more elegant?
a solution use same onfragmentinteractionlistener fragments, , use 1 param of each listener methods (like tag parameter) identificate fragment sent action.
here example:
make new class , every fragment use class
onfragmentinteractionlistener.java
public interface onfragmentinteractionlistener { public void onfragmentmessage(string tag, object data); }
in activity:
public void onfragmentmessage(string tag, object data){ if (tag.equals("tagfragment1")){ //do 'data' comes fragment1 } else if (tag.equals("tagfragment2")){ //do 'data' comes fragment2 } ... }
you can use object type pass every type of data want ( then, in every if, must convert object type necessary).
using way, maintenance easier have 6 differents listeners , method every type of data want pass.
hope helps.
Comments
Post a Comment