android - Fragment callbacks explosion, how to deal? -
i creating app using fragments. have main activity, has framelayout root layout hold fragments.
after thought have decided separate application logic in several parts, example : mainactivity responsible app basic navigation (mainpagefragment, categorylistfragment, productlistfragment, productdescriptionfragment), authactivity responsible autherization, registration (signinfragment, registrationfragment, recoverpasswordfragment).
little app. if have recommendation or don't agree app structure, grateful critics.
what problem, can see mainactivity has many responsibilities. there 4 fragments can more in future.
lets consider next situation. in mainactivity have mainpagefragment , fragment in turn of course has views. , on click event need change fragment, instance mainpagefragment categorylistfragment. in case have several ways handle clicks or other events framgents.
the common way have activity implements callback interface defined in fragment class nested class inteface. approach quite , easy use. if host activity has handle multiple callbacks fragments, more, there can more 1 callback single fragment, class(activity) declaration starts growing, class body too. possible approaches solve problem.
you can handle clicks, events directly inside fragment (start activity, replace framgent......) can painless, me callback approach looks better, maybe there nothing bad, , can use approach.
use 1 or several interfaces getting information fragments. example create class
callbackeventholding such infoframgentid, eventtype.... using approach can reduce interfaces , methods, activity class body can become larger in first approach.use eventbus pattern communicate between app components via third party service.
of course there other ways this, have described popular.
please suggest, or explain how solve problem in apps, approach better, how built communication easy maintain.
i grateful advice,critics in advance.
- if app becomes more complex using callback pattern messy, if fragments need communicate fragments. i'd use pattern apps low complexity (activity, 1 or 2 fragments).
- clicks, events etc. should handled inside fragment if whatever happens stays within fragment. don't replace fragment within fragment, that's activity's responsibility. might easier getactivity().somemethod in fragment leads hard maintain code. might understand it's doing struggle in half year.
- this approach looks messy me (similar 1.)
- that's 1 i'd recommend. i'm using eventbus (https://github.com/greenrobot/eventbus) there alternative implementations otto (https://github.com/square/otto) , i've never looked times when used callback pattern. using eventbus decouples communication between different components , code becomes simpler , leaner. need careful approach since there pitfalls. 1 gets easier communicate component other component lead messier code listener/observer pattern. 1 events asynchronous compared synchronous listener calls need make sure you're receiving "right" events @ right moment in component's lifecycle. main advantages of eventbus approach imo:
- a message object forcing developer code object oriented compared more functional listener method calls
- it decouples different components. publisher , subscribers don't have know each other. decoupling components make code leaner , easier read (and maintain).
- it can used arbitrary components. e.g. replaced localbroadcastmanager calls eventbus messages (eventbus faster using localbroadcastmanager). being able communicate between arbitrary components convenient if components can't access each other directly (like dialog , preference object)
Comments
Post a Comment