Binding and unbinding Android Service to/from Fragment (in ViewPager) -
i got 1 single main-activity in create dynamically pages inside viewpager (via fragmentpageradapter). far good.
now got service-implementation want activity/those fragments bind to. , point ask myself best practice.
- binding activity , communicate fragment service via callback through activity? (binding/unbinding in oncreate()/ondestroy())
- binding each fragment on creation , unbind when destroyed? (same above)
both implementations seem work. second 1 offers straighter communication service.
when testing second fragments bound service on creation. when remove 1 doesn't seem destroyed. when start create fragments point (only 1 fragment after deleting others) again, binding service takes place when create more fragments created before.
perhaps again better overview.
- create activity 1 fragment in pager: service connected fragment
- create 2 more fragments in pager: service conntected on each creation
- removing 1 fragment: unbinding seems not called
- create 2 more fragments again: service seems not connected
- create fragment: service conntected on creation
is behaviour 1 can expect? implementation should choose; straighter communication on callback-communication?
thanks input in advance!
i prefer first method if use local service because have handle service.
assume activity has reference local service called "mylocalservice" (checkout local service example). within fragment can call like: getactivity().getmylocalservice(). thing fragment doesn't have deal lifecycle of service. hope helps.
Comments
Post a Comment