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

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -